Codux is a state management library inspired by Redux.
You can list all the events that will happen in your app.
You can store the current state of a particular model separately.
You should finish all the job with side effects before changing the state.
You can also specify when your stores and effects is constructed and destoryed.
flutter pub add codux
class Increment implements Event<void> {
const Increment();
}
class CounterStore extends Store<int> {
CounterStore() : super(initialState: 0) {
on<Increment>((current, event) {
return current.state + 1;
});
on<Decrement>((current, event) {
return current.state - 1;
});
}
}
class FindMoviesEffect extends Effect {
FindMoviesEffect() {
on<FindMovies>((event) {
Client.get("/movies").then((data) {
dispatch(MoviesFound(data));
});
});
}
}
class MovieListPage extends Component {
const MovieListPage({super.key});
@override
void onCreated(BuildContext context) {
useStore(() => MovieListStore());
useEffect(() => FindMoviesEffect());
super.onCreated(context);
}
@override
Widget render(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: find<MovieListStore>().stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView(
children: [
for (final element in snapshot.data)
MovieListTile(element)
]
);
}
},
),
);
}
}