Flutter Bloc-小部件树不断生长

时间:2020-02-15 23:54:57

标签: flutter bloc flutter-bloc

当我从最终视图返回到“初始视图”时,所有3 State中的每个BlocProviders被保存。

请参阅小部件树: Widget Tree

我的代码在这里链接: GIST

我不知道自己缺少什么,在第一次运行时一切正常。

但是,当我回到InitialView时回到GameView时,请重新设置所有内容并再次移至GameModeSelectionView,它被多次调用(状态以某种方式保存而不是转储??)(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

此过程随着每次迭代(1)

我正在使用flutter_bloc依赖项: https://pub.dev/packages/flutter_bloc

1 个答案:

答案 0 :(得分:0)

我使用MaterialPageRoute onGenerateRoute解决了该问题(因为我需要动态路由)

我将Buttons onPressed方法中的代码替换为:

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

像魅力一样运转,不再生长一棵树。