Flutter Bloc – Widget tree growing endlessy

Issue

When I’m going back from the final view to my “initial view” every State of all 3 BlocProviders is being saved.

See Widget tree:
Widget Tree

My Code is linked here:
GIST

I don’t understand what I am missing, on first run everything works fine.

However, when I go back to InitialView when I’m in GameView, set everything up again and move to the GameModeSelectionView again, its getting called multiple times (States are somehow saved instead of dumped??)(1):

flutter: State of GameSelectionState GameSelectionInitial
flutter: State of GameSelectionState GameSelectionStart
flutter: State of QuestionState StopQuestion
flutter: OK?
flutter: State of GameSelectionState GameSelectionStart
flutter: State of QuestionState StopQuestion
flutter: OK?
flutter: State of GameSelectionState GameSelectionStart
flutter: State of QuestionState StopQuestion
flutter: OK?
flutter: State of GameSelectionState GameSelectionInitial

This process grows with each iteration (1)

I am using the flutter_bloc dependency:
https://pub.dev/packages/flutter_bloc

Solution

I fixed the issue using MaterialPageRoute onGenerateRoute (since I need to route dynamically)

I replaced the code in my Buttons onPressed method to like:

Navigator.of(context).pushReplacementNamed(
              GameModeSelectionView.routeName,
              arguments: players);

Works like a charm and no more growing tree.

Answered By – 0x45

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.