Stores & Effects
Model persistent feature state with Store, commit, effects, and .ui().
Stores and Effects
A Store owns persistent state and behavior:
class ProfileStore extends Store<ProfileState> {
ProfileStore(this.repository) : super(const ProfileState());
final ProfileRepository repository;
Future<void> refresh() async {
final nextState = await repository.load();
commit(nextState);
}
}
Declare:
final profile = Popsicle.create(
(scope) => ProfileStore(
scope.get(profileRepository),
),
);
Render:
profile.ui(
(context, state, store) {
return ProfileBody(
state: state,
onRefresh: store.refresh,
);
},
effect: (context, effect) {
// snackbar/navigation/dialog/etc.
},
);
commit
Use commit(...) for persistent state transitions:
commit(nextState);
effect
Use effect(...) for one-shot occurrences:
effect(const ProfileSaved());
Effects are not state, are not replayed on rebuild, do not rebuild Store UI by themselves, and are not included in History.