Flutter Statefull小部件状态在按下后重建
我有一个奇怪的问题: 我有一个像这样的小部件Flutter Statefull小部件状态在按下后重建,flutter,statefulwidget,Flutter,Statefulwidget,我有一个奇怪的问题: 我有一个像这样的小部件 class ActionDetailView extends StatefulWidget { static const routeName = '/actionDetails'; @override _ActionDetailViewState createState() { print("Create state"); return _ActionDetailViewState(); } } class _Ac
class ActionDetailView extends StatefulWidget {
static const routeName = '/actionDetails';
@override
_ActionDetailViewState createState() {
print("Create state");
return _ActionDetailViewState();
}
}
class _ActionDetailViewState extends State<ActionDetailView>
with WidgetsBindingObserver {
ActionDetailBloc _bloc = ActionDetailBloc();
_ActionDetailViewState() {
print("ActionDetailViewState constructed");
}
@override
void didChangeDependencies() {
print("DidChangeDependencicse");
final String actionUID = ModalRoute.of(context).settings.arguments;
_bloc.init(actionUID);
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(...)
}
所以从日志来看,小部件的状态似乎是在未收到任何新路由的情况下重建的,所以它会造成问题,因为调用了dispose(),并且BLoC中的所有流都已关闭。StreamBuilder根据流数据和体内多个小部件或无状态小部件以两种状态之一返回Scaffold。没有任何地方被称为setState()。导航器通过pushRouteName()打开了小部件
我的问题是:是什么导致了这种奇怪的行为以及如何防止它?想法是在每次推送route创建小部件时创建bloc(它包含上下文数据,所以singleton不太好)是的,目前他们正在修复它。这是颤振 链接中还提到了一个解决方法 副本:
============= Enter to view first time =======
2020-01-08 09:53:36.760 5960-5987/ I/flutter: View action //pushed route from another place in code
2020-01-08 09:53:36.808 5960-5987/ I/flutter: Create state //constructor of State is called
2020-01-08 09:53:36.810 5960-5987/ I/flutter: ActionDetailViewState constructed //confirmation of above
2020-01-08 09:53:36.811 5960-5987/ I/flutter: DidChangeDependencicse
2020-01-08 09:53:36.812 5960-5987/ I/flutter: init actionDetailBloc //bloc init method for fill streams
2020-01-08 09:53:36.864 5960-5987/ I/flutter: LoadingUsers //loading state
2020-01-08 09:53:38.253 5960-5987/ I/flutter: pushing to stream
2020-01-08 09:53:38.276 5960-5987/ I/flutter: ReceivedList //data received
=============== Back pressed ====
2020-01-08 09:53:40.870 5960-5987/ I/flutter: DidChangeDependencicse //suddenly it is rebuilt despite not visible
2020-01-08 09:53:40.870 5960-5987/ I/flutter: init actionDetailBloc
2020-01-08 09:53:40.884 5960-5987/ I/flutter: ReceivedList
2020-01-08 09:53:42.160 5960-5987/ I/flutter: pushing to stream