dart GetX未处理的异常:构建期间调用setState()或NeedmarksBuild()-当转到屏幕时

s3fp2yjn  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(133)

我正在使用GetX。
当我从事件屏幕转到主屏幕-〉一切正常...然后返回事件屏幕和主屏幕(使用左侧的抽屉菜单)...第二次返回主屏幕时,出现此错误:

[GETX] Instance "EventsScreenController" has been initialized
[GETX] GOING TO ROUTE /HomeScreen
[GETX] Instance "HomeScreenController" has been created
[GETX] Instance "HomeScreenController" has been initialized
[GETX] GOING TO ROUTE /EventsScreen
[GETX] GOING TO ROUTE /HomeScreen
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: setState() or markNeedsBuild() called during build.
This 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.
The widget on which setState() or markNeedsBuild() was called was:
  Obx
The widget which was currently being built when the offending call was made was:
  Builder
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4634:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4646:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1153:15)
#3      _ObxStat<…>

为什么我第二次进入主屏幕时会出现这种情况?
主屏幕中的一些代码:

child: Obx(
                () {
                  return controller.isLoading.value
                      ? const Center(child: CircularProgressIndicator())
                      : controller.isError.value
                          ? ErrorCard(
                              controller: controller,
                              retryFunction: () => controller.getEvents(),
                            )
                          : controller.events.isEmpty
                              ? const Text(
                                  "There are no events available for the defined filter") //TODO: Show ErrorCard and set retryFuntion: openFilter of events (popup)
                              : ListView.builder(
.......)})
knpiaxh1

knpiaxh11#

试试这个

child: Obx(
            () {
      if(controller.isLoading.value == true){
        return const Center(child: CircularProgressIndicator());
      }      
       return controller.isError.value
                      ? ErrorCard(
                          controller: controller,
                          retryFunction: () => controller.getEvents(),
                        )
                      : controller.events.isEmpty
                          ? const Text(
                              "There are no events available for the defined filter") //TODO: Show ErrorCard and set retryFuntion: openFilter of events (popup)
                          : ListView.builder(
....)})

相关问题