skm*_*skm -2 state-management dart flutter flutter-getx
我正在使用 GetX。
\n当我从 EventScreen 转到主屏幕 -> 一切正常...然后返回 EventScreen 并返回主屏幕(使用左侧的抽屉菜单)...第二次返回主屏幕时,会发生此错误:
\n[GETX] Instance "EventsScreenController" has been initialized\n[GETX] GOING TO ROUTE /HomeScreen\n[GETX] Instance "HomeScreenController" has been created\n[GETX] Instance "HomeScreenController" has been initialized\n[GETX] GOING TO ROUTE /EventsScreen\n[GETX] GOING TO ROUTE /HomeScreen\n[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: setState() or markNeedsBuild() called during build.\nThis Obx widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.\nThe widget on which setState() or markNeedsBuild() was called was:\n Obx\nThe widget which was currently being built when the offending call was made was:\n Builder\n#0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4634:9)\n#1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4646:6)\n#2 State.setState (package:flutter/src/widgets/framework.dart:1153:15)\n#3 _ObxStat<\xe2\x80\xa6>\nRun Code Online (Sandbox Code Playgroud)\n为什么我第二次进入主屏幕时会出现这种情况?
\n主屏幕中的一些代码:
\n child: Obx(\n () {\n return controller.isLoading.value\n ? const Center(child: CircularProgressIndicator())\n : controller.isError.value\n ? ErrorCard(\n controller: controller,\n retryFunction: () => controller.getEvents(),\n )\n : controller.events.isEmpty\n ? const Text(\n "There are no events available for the defined filter") //TODO: Show ErrorCard and set retryFuntion: openFilter of events (popup)\n : ListView.builder(\n.......)})\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
如果您使用的是有状态小部件,您正在 initstate 方法上更新您的值,请使用此方法。
initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
// executes after build
});
}
Run Code Online (Sandbox Code Playgroud)
注意:- 您正在构建小部件之前更新值。
| 归档时间: |
|
| 查看次数: |
828 次 |
| 最近记录: |