For example in a Todo App, but works for all type of apps
You have a TodosBloc that have events like LoadTodos, addTodo, DeleteTodo, ...
You have a TodosPage that show a list of all Todos, and have a DetailsPage that show all information of one todo selected.
When you select a Todo in TodosPage I want to show this Todo in DetailsPage
I see two approach for do that, but i don't know what is the best for BLoC, or maybe you can see other approach
1. pass a Todo like argument to DetailsPage
...
itemBuilder: (BuildContext context, int index) {
final todo = todos[index];
return TodoItem(
todo: todo,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) {
return DetailsScreen(id: todo.id);
}),
);
}
);
...
it is so simple, but i don't like that widgets pass this arguments, because all logic is better in a bloc, I think...
2. Set todo in a var in TodosBlocState
...
class TodoSelected extends TodosState {
final Todo todo;
TodoSelected(this.todo);
}
...
```dart
....
class SelectTodo extends TodosEvent {
final Todo todo;
SelectTodo(this.todo);
}
...
```dart
....
Stream<TodosState> mapEventToState(
TodosState currentState,
TodosEvent event,
) async* {
if (event is SelectTodo) {
yield TodoSelected(event.todo);
}
...
}
...
...
itemBuilder: (BuildContext context, int index) {
final todo = todos[index];
return TodoItem(
todo: todo,
onTap: () {
todosBloc.dispatch(TodoSelected(todo));
Navigator.of(context).push(
MaterialPageRoute(builder: (_) {
return DetailsScreen();
}),
);
}
);
...
@basketball-ico great question! 馃憤
I think both options are viable as they get the job done and are testable but I slightly prefer the 1st option because it is the "stateless" approach. I think by not having to persist the selected todo we reduce the complexity of the application and I personally tend to always pick the solution that involves maintaining as little state as possible. Let me know if that helps 馃槃
Also, you can check out the bloc library todo app in the flutter architecture samples for a concrete implementation.
@basketball-ico closing this for now but feel free to comment and I'll gladly reopen it 馃憤
Most helpful comment
@basketball-ico great question! 馃憤
I think both options are viable as they get the job done and are testable but I slightly prefer the 1st option because it is the "stateless" approach. I think by not having to persist the selected todo we reduce the complexity of the application and I personally tend to always pick the solution that involves maintaining as little state as possible. Let me know if that helps 馃槃
Also, you can check out the bloc library todo app in the flutter architecture samples for a concrete implementation.