【发布时间】:2020-12-05 07:33:13
【问题描述】:
更改设备方向时出现以下错误:
E/flutter (31741): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: setState() called after dispose(): _StreamBuilderBaseState<List<ScheduledActivity>, AsyncSnapshot<List<ScheduledActivity>>>#afadb(lifecycle state: defunct, not mounted)
E/flutter (31741): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (31741): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (31741): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (31741): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1204:9)
E/flutter (31741): #1 State.setState (package:flutter/src/widgets/framework.dart:1239:6)
E/flutter (31741): #2 _StreamBuilderBaseState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:140:9)
E/flutter (31741): #3 _StartWithValueStream.listen.<anonymous closure> (package:moor/src/utils/start_with_value_transformer.dart:49:17)
E/flutter (31741): #4 _rootRun (dart:async/zone.dart:1182:47)
E/flutter (31741): #5 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (31741): #6 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (31741): #7 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (31741): #8 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (31741): #9 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (31741): #10 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (31741): #11 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (31741): #12 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (31741): #13 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (31741):
现在,我知道about the if (mounted) setState fix - 但是,我没有在我的应用程序中使用setState,因为我使用的是bloclibrary。
另外,查看错误,我可以看到StreamBuilder<List<ScheduledActivity>>,这是导致问题的以下代码sn-ps的唯一线索:
...
@override
Widget build(BuildContext context) {
return StreamBuilder<List<ScheduledActivity>>(
stream: RepositoryProvider.of<AppDatabase>(context)
.scheduledActivitiesDao
.watchActivitiesByPlant(widget.plantId),
builder: (context, snapshot) {
if (snapshot.hasError) print('>>>> DB error - ${snapshot.error}');
if (!snapshot.hasData) return ProgressIndicatorWidget();
return Text('items = ${snapshot.data.length}');
},
);
}
...
上面代码的两个注意事项:
- 上面我发布的
build函数的Widget是StatefulWidget,但是,即使我将它重构为StatelessWidget,我也会遇到同样的错误 - 仅当我从 横向 切换到 纵向 模式时才会出现此问题。
【问题讨论】: