状态完整小部件状态在后按后重建



我有一个奇怪的问题: 我有这样的状态全小部件

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(...)
}

我的日志如下所示:

============= 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

因此,从日志来看,小部件的状态是在没有收到任何新路由的情况下重建的,因此它引起了麻烦,因为调用了dispose((并且BLoC中的所有流都已关闭。StreamBuilder 根据流数据返回两种状态之一的基架,并在正文中返回多个 Widgets 或 StatelessWidget。没有任何地方被称为 setState((。Widget 由 Navigator 通过 pushRouteNamed(( 打开。

我的问题是:是什么导致了这种奇怪的行为以及如何防止它?想法是每次通过推送路由创建小部件时创建 bloc(它包含上下文数据,所以单例不会很好(

是的,目前他们将修复它。这是颤振 问题链接

链接中还提到了一种解决方法。

副本: 如何处理不需要的小部件构建?

最新更新