You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This sample makes use of the [bloc](https://pub.dartlang.org/packages/bloc) and [flutter_bloc](https://pub.dartlang.org/packages/flutter_bloc) libraries to manage state.
4
+
5
+
Check out the [bloc library documentation](https://felangel.github.io/bloc) for more details and tutorials.
6
+
7
+
## Key Concepts
8
+
9
+
* Lift State Up
10
+
* If a bloc is needed by multiple widgets provide it using `BlocProvider` at the greatest common ancestor
11
+
*`Events` are the input to a bloc.
12
+
* They are commonly dispatched in response to user interactions such as button presses or lifecycle events like page loads.
13
+
*`States` are the output of a bloc and represent a part of your application's state.
14
+
* components can be notified of states and redraw portions of themselves based on the current state.
15
+
* The change from one state to another is called a `Transition`.
16
+
* A `Transition` consists of the current state, the event, and the next state.
17
+
* A bloc converts a `Stream` of incoming `Events` into a `Stream` of outgoing `States`
18
+
19
+
## Updating App State
20
+
21
+
Every bloc has a `dispatch` method. `Dispatch` takes an event and triggers `mapEventToState`. Dispatch notifies the bloc of a new event which will then trigger a state change.
22
+
23
+
## Sharing Data between Blocs
24
+
25
+
In this implementation, the `FilteredTodosBloc` depends on the `TodosBloc` and will update the list of filtered todos in response to changes in the `TodosBloc`.
26
+
This approach demonstrates how we can build a reactive application by composing blocs from other blocs. On app start, the `TodosBloc` is hydrated with todos from the repository and from that moment forward, all mutations to todos are executed through the `TodosBloc` which persists the changes asynchronously. The `FilteredTodosBloc` listens for changes in the `TodosBloc` state and will update it's list of filtered todos. The result is, a hierarchy of states with all todos being managed by the `TodosBloc` and a subset of those todos (filtered todos) being managed by the `FilteredTodosBloc`.
27
+
28
+
## Updating UI
29
+
30
+
`BlocBuilder` is a Flutter widget which requires a bloc and a builder function. `BlocBuilder` handles building the widget in response to new states. `BlocBuilder` is very similar to `StreamBuilder` but has a simplified API to reduce the amount of boilerplate code needed and is also optimized to avoid unnecessary rebuilds.
31
+
32
+
## Testing
33
+
34
+
Generally, this app conforms the "Testing Pyramid": Lots of Unit tests, fewer Widget tests, and fewer integration tests.
35
+
36
+
* Unit tests
37
+
-`Blocs` can easily be tested by mocking dependencies with `Mockito`. `Events` can be dispatched and we can assert that the blocs' state has changed correctly.
38
+
* Widget Tests
39
+
- Widgets can be tested by passing in fake data and making assertions against the Widget rendered with that data.
40
+
* Integration Tests
41
+
- Run the app and drive it using flutter_driver `flutter drive --target test_driver/todo_app.dart`.
0 commit comments