Events
An action may need to trigger a one-time side effect in the UI, such as opening a dialog, hiding the keyboard, clearing a text field, or scrolling a list back to the top.
Some Flutter widgets expose a controller for these operations. For example, you can
call clear() on a TextEditingController to clear a TextField, or jumpTo(0) on a
ScrollController to scroll a ListView to the top.
final textController = TextEditingController();
TextField(controller: textController);
final scrollController = ScrollController();
ListView(
controller: scrollController,
children: [
// List items...
],
);
Keep controllers in their widgets. Never put them in the store state.
AsyncRedux solves these cases with the Evt class,
which introduces events that can indeed be put into the store state.
Events are single use notifications used to trigger side effects in widgets.
They differ from regular state values because once read they are automatically consumed,
so they only trigger once.
When should I use events?
Some guidelines:
- Use events to update the internal state of stateful widgets, after they are built.
- Use events for one-off controller actions, like clearing text, changing focus, scrolling, or triggering an animation.
- Use events for one-off implicit UI state, like opening dialogs, showing snackbars, toggling the keyboard, or navigating.
- If you need to read the same value more than once, it should probably be regular store state, not an event.
- Also use regular store state when widgets need to access state that's external to them.
But the short answer is that you'll know it when you see it. When you want something to happen, and it's not clear how to do it with regular store state, events are often the easier way to solve it.
Setting up
Make sure you included the following extension code in your app, as shown in Using the store state:
extension BuildContextExtension on BuildContext {
AppState read() => getRead<AppState>();
R select<R>(R Function(AppState st) selector) => getSelect<AppState, R>(selector);
R? event<R>(Evt<R> Function(AppState st) selector) => getEvent<AppState, R>(selector);
}
In special, note the context.event extension method above
that you will use to consume events in your widgets.
Creating events
Use the Evt suffix when naming events.
For example, an event that clears text can be named clearTextEvt.
A boolean event is created with the Evt() constructor, with no value and no generic
type.
In this case, the event returns true once, and then false on later calls.
var clearTextEvt = Evt();
Events can also hold a value of any type by using Evt<T>(value).
In this case, the event returns the value once and null on later calls.
var changeNameEvt = Evt<String>("Mark");
var changeAgeEvt = Evt<int>(42);
Declare spent events in the initial state
Declare the events in your state class and initialize them as "spent"
by using Evt.spent() or Evt<T>.spent().
class AppState {
final Evt clearTextEvt;
final Evt<String> changeTextEvt;
AppState({required this.clearTextEvt, required this.changeTextEvt});
static AppState initialState() => AppState(
clearTextEvt: Evt.spent(), // Here!
changeTextEvt: Evt<String>.spent(), // Here!
);
}
Actions place unspent events in the state
Actions then create new unspent events, placing them in the state:
// Boolean event.
class ClearText extends AppAction {
AppState reduce() => state.copy(clearTextEvt: Evt()); // Here!
}
// Event with a String payload.
class ChangeText extends AppAction {
Future<AppState> reduce() async {
String newText = await fetchTextFromApi();
return state.copy(changeTextEvt: Evt<String>(newText)); // Here!
}
}
Using events
Use context.event((st) => ...) to consume the event in the widget build method.
It will get the value of the event only once, and then false/null on later calls:
Widget build(BuildContext context) {
// Gets `true` once, then `false` on later calls.
bool clearText = context.event((st) => st.clearTextEvt);
if (clearText) controller.clear();
// Gets the String value once, then `null` on later calls.
String? newText = context.event((st) => st.changeTextEvt);
if (newText != null) controller.text = newText;
...
You can also consume events in the didChangeDependencies method of a StatefulWidget.
It runs again when the event changes, before build.
Note each event can be consumed by only one widget. If more than one widget must react to the same trigger, create separate events in the state.
You should never persist events when saving the app state to the local storage of the device.
Try running the: Evt Example.
This concludes the basics of AsyncRedux. You now know how to create and read the state, dispatch actions to change the state, run asynchronous actions, show spinners when actions are running, and error messages when they fail. That is enough for you to be productive with AsyncRedux, and create your own apps with it.
However, if you want to become an advanced AsyncRedux user, continue reading the next sections. The next one will cover advanced topics related to actions.