Bloc: Bloc looses current state for initial state

Created on 22 Jul 2019  路  2Comments  路  Source: felangel/bloc

Describe the bug
I've created a very similar app as the Todo Example app. I don't use the localizations and I have created the routes as such:

routes: <String, WidgetBuilder>{
          "/": (BuildContext context) {
            return MultiBlocProvider(
              providers: [
                BlocProvider<TabBloc>(
                  builder: (context) => TabBloc(),
                ),
                BlocProvider<FilteredTodosBloc>(
                  builder: (context) =>
                      FilteredTodosBloc(todosBloc: todosBloc),
                ),
              ],
              child: HomeScreen(),
            );
          },
          "/addTodo": (BuildContext context) {
            TodoBloc().dispatch(AddTodo());
            return AddTodoScreen();
          },

The app loads fine, and filters the flights just fine. But when I navigate to /addTodo a new entry doesn't get added. An empty entry with just an id should be added when I navigate to this route. I've done some debugging and everything works. The only problem is that the state when the bloc goes to AddTodo is not TodosLoaded() as it was before this got called, but for some reason it went back to its initial state of TodosLoading(). I've checked the state of Todos throughout filteredTodos and it seems to stay as TodosLoaded() and it only changes when I navigate to /addTodo.

Expected behavior
The state should be TodosLoaded.

*Logs *
No errors with flutter analyze

[flutter] flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[鈭歖 Flutter (Channel stable, v1.7.8+hotfix.3, on Microsoft Windows [Version 10.0.18362.239], locale en-US)
[鈭歖 Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] Android Studio (version 3.3)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[鈭歖 VS Code (version 1.36.1)
[鈭歖 Connected device (1 available)

! Doctor found issues in 1 category.
exit code 0

question

Most helpful comment

You're right. I was creating a new declaration instead of using that by the blocProvider. Sorry, I was tired. Thanks

All 2 comments

Hi @charlesblack 馃憢
Thanks for opening an issue!

It sounds like you're creating multiple instances of the TodosBloc instead of using the same instance across of the routes. Are you able to share a link to the application so that I can run/debug it locally?

Thanks! 馃憤

You're right. I was creating a new declaration instead of using that by the blocProvider. Sorry, I was tired. Thanks

Was this page helpful?
0 / 5 - 0 ratings