- Most of the Key Concepts from the Redux Example apply to this example as well, but the implementations are slightly different.
- To enforce immutability,
built_reduxapps require you to use a
- To increase discoverability, all actions are created using
built_reduxand attached to the
- To use
built_redux, you must add a
build.yamlfile to your project.
- To help with Type Safety, Reducers and Middleware can be created with
State objects in your app need to be created with
built_value is a library that generate "Value Classes" from a Class template that you write.
The Value classes can not be directly modified, but instead must be updated by creating a new version of the object.
One benefit of
built_redux is that it attaches all possible actions to your store. This makes it very easy to see which actions are available for dispatch within your IDE using autocompletion.
In order to use
built_value, you need to create a
build.yaml file in your project. Whenever you update your Value Classes or Redux Actions you'll need to run the build command:
flutter pub pub run build_runner build. Instead of running the
build command, you can run the
flutter pub pub run build_runner watch. This will watch for changes and trigger a rebuild every time you make updates. This tends to be much faster overall.
Type Safety in Reducers and Middleware
As your app grows, you'll want to break reducers and middleware down into smaller functions.
Differences to Redux
These two libraries are incredibly similar. These are the minor differences:
built_redux- Actions are generated for you by
built_reduxbased on a definition. They are then attached to the Store upon creation. Each action has a unique name and a generic payload type. Each action can have at most one reducer.
Redux, Actions are plain ol' Dart values, Classes or Enums.
built_redux- Reducers are void functions that mutate a
StateBuilderis then built after all reducers have run. Enforces immutability.
redux- Reducers are functions in app state and latest action and return a new app state. Since immutability is not enforced, a user could simply mutate the state object instead of returning an updated copy.
- Both - Testing is easy, and both libraries have utilities for binding Reducers to Actions of a specific type.
- Very little difference here. Both libraries have utilities for binding actions of a specific type to a given Middleware.
- Nesting Large State Trees
built_redux- Provides helpers for composing large Action trees that you can attach to your Store upon creation. Reducers can be combined via functional composition and by using utilities from the library.
redux- No need for nesting actions, nesting reducers can be done via functional composition and by using utilities from the library.
- Both - allow you to break down your app into smaller units.
- Flutter integration
built_redux- Maps from a
Prop, which is passed to your
buildmethod along with your
Actions. You combine the
Propwih the actions in the
redux- Maps from a
ViewModelshould include both "Props" and callback functions that dispatch actions.
- Both - Store a Widget at the top of your tree containing your State.