GetBuilder<GamesController> 小部件无法标记为需要构建,因为框架已在进程中

Ber*_*kan 2 setstate flutter

我试图从 json 获取数据,但出现一些错误,但我不知道为什么。

\n

我收到这个错误:

\n
E/flutter ( 4560): [ERROR:flutter/lib/ui/ui_dart_state.cc(207)] Unhandled Exception: setState() or markNeedsBuild() called during build.\nE/flutter ( 4560): This GetBuilder<GamesController> 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.\nE/flutter ( 4560): The widget on which setState() or markNeedsBuild() was called was:\nE/flutter ( 4560):   GetBuilder<GamesController>\nE/flutter ( 4560): The widget which was currently being built when the offending call was made was:\nE/flutter ( 4560):   _BodyBuilder\nE/flutter ( 4560): #0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4305:11)\nE/flutter ( 4560): #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4320:6)\nE/flutter ( 4560): #2      State.setState (package:flutter/src/widgets/framework.dart:1108:15)\n
Run Code Online (Sandbox Code Playgroud)\n

和我的代码:

\n
Future getLeagues(var sportCategory) async {\n//update();\nmyArrayA.clear();\nmyArrayB.clear();\nmyArrayC.clear();\nmyArrayCC.clear();\nmyArrayD.clear();\nmyArrayE.clear();\nmyArrayF.clear();\nmyArrayG.clear();\nmyArrayGG.clear();\nmyArrayH.clear();\nmyArrayII.clear();\nmyArrayI.clear();\nmyArrayJ.clear();\nmyArrayK.clear();\nmyArrayL.clear();\nmyArrayM.clear();\nmyArrayN.clear();\nmyArrayO.clear();\nmyArrayOO.clear();\nmyArrayP.clear();\nmyArrayR.clear();\nmyArrayS.clear();\nmyArraySS.clear();\nmyArrayT.clear();\nmyArrayU.clear();\nmyArrayUU.clear();\nmyArrayV.clear();\nmyArrayY.clear();\nmyArrayZ.clear();\n\nleagueList.clear();\nleagueEnum =  LeagueEnum.Loading;\nupdate();\n\n\n_apiServices.getLeagues(sportCategory).then((value) {\n  if (value != false) {\n    allLeagues = LeagueWCountryModel.fromJson(value);\n    //A\n    if (allLeagues.others.a != null)\n      allLeagues.others.a.forEach((element) {\n        allCountriesA.contains(element.category.name)\n            ? null\n            : allCountriesA.add(element.category.name);\n      });\n    //B\n    if (allLeagues.others.b != null)\n      allLeagues.others.b.forEach((element) {\n        allCountriesB.contains(element.category.name)\n            ? null\n            : allCountriesB.add(element.category.name);\n      });\n    //C\n\n    if (allLeagues.others.c != null)\n      allLeagues.others.c.forEach((element) {\n        allCountriesC.contains(element.category.name)\n            ? null\n            : allCountriesC.add(element.category.name);\n      });\n    //\xc3\x87\n    if (allLeagues.others.cc != null)\n      allLeagues.others.cc.forEach((element) {\n        allCountriesCC.contains(element.category.name)\n            ? null\n            : allCountriesCC.add(element.category.name);\n      });\n    //D\n    if (allLeagues.others.d != null)\n      allLeagues.others.d.forEach((element) {\n        allCountriesD.contains(element.category.name)\n            ? null\n            : allCountriesD.add(element.category.name);\n      });\n    //E\n    if (allLeagues.others.e != null)\n      allLeagues.others.e.forEach((element) {\n        allCountriesE.contains(element.category.name)\n            ? null\n            : allCountriesE.add(element.category.name);\n      });\n    //F\n    if (allLeagues.others.f != null)\n      allLeagues.others.f.forEach((element) {\n        allCountriesF.contains(element.category.name)\n            ? null\n            : allCountriesF.add(element.category.name);\n      });\n    //G\n    if (allLeagues.others.g != null)\n      allLeagues.others.g.forEach((element) {\n        allCountriesG.contains(element.category.name)\n            ? null\n            : allCountriesG.add(element.category.name);\n      });\n    //GG\n    if (allLeagues.others.gg != null) if (allLeagues.others.gg != null)\n      allLeagues.others.gg.forEach((element) {\n        allCountriesGG.contains(element.category.name)\n            ? null\n            : allCountriesGG.add(element.category.name);\n      });\n    //H\n    if (allLeagues.others.h != null)\n      allLeagues.others.h.forEach((element) {\n        allCountriesH.contains(element.category.name)\n            ? null\n            : allCountriesH.add(element.category.name);\n      });\n\n    //II\n    if (allLeagues.others.ii != null)\n      allLeagues.others.ii.forEach((element) {\n        allCountriesII.contains(element.category.name)\n            ? null\n            : allCountriesII.add(element.category.name);\n      });\n    //\xc4\xb0\n    if (allLeagues.others.i != null)\n      allLeagues.others.i.forEach((element) {\n        allCountriesI.contains(element.category.name)\n            ? null\n            : allCountriesI.add(element.category.name);\n      });\n    //J\n    if (allLeagues.others.j != null)\n      allLeagues.others.j.forEach((element) {\n        allCountriesJ.contains(element.category.name)\n            ? null\n            : allCountriesJ.add(element.category.name);\n      });\n    //K\n    if (allLeagues.others.k != null)\n      allLeagues.others.k.forEach((element) {\n        allCountriesK.contains(element.category.name)\n            ? null\n            : allCountriesK.add(element.category.name);\n      });\n    //L\n    if (allLeagues.others.l != null)\n      allLeagues.others.l.forEach((element) {\n        allCountriesL.contains(element.category.name)\n            ? null\n            : allCountriesL.add(element.category.name);\n      });\n    //\xc4\xb0\n    if (allLeagues.others.m != null)\n      allLeagues.others.m.forEach((element) {\n        allCountriesM.contains(element.category.name)\n            ? null\n            : allCountriesM.add(element.category.name);\n      });\n    //N\n    if (allLeagues.others.n != null)\n      allLeagues.others.n.forEach((element) {\n        allCountriesN.contains(element.category.name)\n            ? null\n            : allCountriesN.add(element.category.name);\n      });\n    //O\n    if (allLeagues.others.o != null) if (allLeagues.others.o != null)\n      allLeagues.others.o.forEach((element) {\n        allCountriesO.contains(element.category.name)\n            ? null\n            : allCountriesO.add(element.category.name);\n      });\n    //OO\n    if (allLeagues.others.oo != null)\n      allLeagues.others.oo.forEach((element) {\n        allCountriesOO.contains(element.category.name)\n            ? null\n            : allCountriesOO.add(element.category.name);\n      });\n    //P\n    if (allLeagues.others.p != null)\n      allLeagues.others.p.forEach((element) {\n        allCountriesP.contains(element.category.name)\n            ? null\n            : allCountriesP.add(element.category.name);\n      });\n    //R\n    if (allLeagues.others.r != null)\n      allLeagues.others.r.forEach((element) {\n        allCountriesR.contains(element.category.name)\n            ? null\n            : allCountriesR.add(element.category.name);\n      });\n    //S\n    if (allLeagues.others.s != null)\n      allLeagues.others.s.forEach((element) {\n        allCountriesS.contains(element.category.name)\n            ? null\n            : allCountriesS.add(element.category.name);\n      });\n    //SS\n    if (allLeagues.others.ss != null) if (allLeagues.others.ss != null)\n      allLeagues.others.ss.forEach((element) {\n        allCountriesSS.contains(element.category.name)\n            ? null\n            : allCountriesSS.add(element.category.name);\n      });\n    //T\n    if (allLeagues.others.t != null)\n      allLeagues.others.t.forEach((element) {\n        allCountriesT.contains(element.category.name)\n            ? null\n            : allCountriesT.add(element.category.name);\n      });\n    //U\n    if (allLeagues.others.u != null)\n      allLeagues.others.u.forEach((element) {\n        allCountriesU.contains(element.category.name)\n            ? null\n            : allCountriesU.add(element.category.name);\n      });\n    //\xc3\x9c\n    if (allLeagues.others.uu != null) if (allLeagues.others.uu != null)\n      allLeagues.others.uu.forEach((element) {\n        allCountriesUU.contains(element.category.name)\n            ? null\n            : allCountriesUU.add(element.category.name);\n      }); //V\n    if (allLeagues.others.v != null)\n      allLeagues.others.v.forEach((element) {\n        allCountriesV.contains(element.category.name)\n            ? null\n            : allCountriesV.add(element.category.name);\n      }); //Y\n    if (allLeagues.others.y != null)\n      allLeagues.others.y.forEach((element) {\n        allCountriesY.contains(element.category.name)\n            ? null\n            : allCountriesY.add(element.category.name);\n      }); //\xc4\xb0\n    if (allLeagues.others.z != null)\n      allLeagues.others.z.forEach((element) {\n        allCountriesZ.contains(element.category.name)\n            ? null\n            : allCountriesZ.add(element.category.name);\n      });\n    //---\n    if (allLeagues.others.a != null)\n      allCountriesA.forEach((element) => {\n            myFilterData = allLeagues.others.a.where((Top x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayA.add(myData)\n          });\n\n    if (allLeagues.others.b != null)\n      allCountriesB.forEach((element) => {\n            myFilterData = allLeagues.others.b.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayB.add(myData)\n          });\n\n    if (allLeagues.others.c != null)\n      allCountriesC.forEach((element) => {\n            myFilterData = allLeagues.others.c.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayC.add(myData)\n          });\n\n    if (allLeagues.others.cc != null)\n      allCountriesCC.forEach((element) => {\n            myFilterData = allLeagues.others.cc.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayC.add(myData)\n          });\n    if (allLeagues.others.d != null)\n      allCountriesD.forEach((element) => {\n            myFilterData = allLeagues.others.d.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayD.add(myData)\n          });\n    if (allLeagues.others.e != null)\n      allCountriesE.forEach((element) => {\n            myFilterData = allLeagues.others.e.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayE.add(myData)\n          });\n\n    if (allLeagues.others.f != null)\n      allCountriesF.forEach((element) => {\n            myFilterData = allLeagues.others.f.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayF.add(myData)\n          });\n    if (allLeagues.others.g != null)\n      allCountriesG.forEach((element) => {\n            myFilterData = allLeagues.others.g.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayG.add(myData)\n          });\n    if (allLeagues.others.gg != null)\n      allCountriesGG.forEach((element) => {\n            myFilterData = allLeagues.others.gg.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayGG.add(myData)\n          });\n    if (allLeagues.others.h != null)\n      allCountriesH.forEach((element) => {\n            myFilterData = allLeagues.others.h.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayH.add(myData)\n          });\n\n    if (allLeagues.others.ii != null) if (allLeagues.others.ii != null)\n      allCountriesII.forEach((element) => {\n            myFilterData = allLeagues.others.ii.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayII.add(myData)\n          });\n    if (allLeagues.others.i != null)\n      allCountriesI.forEach((element) => {\n            myFilterData = allLeagues.others.i.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayI.add(myData)\n          });\n    if (allLeagues.others.j != null)\n      allCountriesJ.forEach((element) => {\n            myFilterData = allLeagues.others.j.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayJ.add(myData)\n          });\n    if (allLeagues.others.k != null)\n      allCountriesK.forEach((element) => {\n            myFilterData = allLeagues.others.k.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayK.add(myData)\n          });\n    if (allLeagues.others.l != null)\n      allCountriesL.forEach((element) => {\n            myFilterData = allLeagues.others.l.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayL.add(myData)\n          });\n    if (allLeagues.others.m != null)\n      allCountriesM.forEach((element) => {\n            myFilterData = allLeagues.others.m.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayM.add(myData)\n          });\n    if (allLeagues.others.n != null)\n      allCountriesN.forEach((element) => {\n            myFilterData = allLeagues.others.n.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayN.add(myData)\n          });\n    if (allLeagues.others.o != null)\n      allCountriesO.forEach((element) => {\n            myFilterData = allLeagues.others.o.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayO.add(myData)\n          });\n    if (allLeagues.others.oo != null)\n      allCountriesOO.forEach((element) => {\n            myFilterData = allLeagues.others.oo.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayOO.add(myData)\n          });\n    if (allLeagues.others.p != null)\n      allCountriesP.forEach((element) => {\n            myFilterData = allLeagues.others.p.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayP.add(myData)\n          });\n    if (allLeagues.others.r != null)\n      allCountriesR.forEach((element) => {\n            myFilterData = allLeagues.others.r.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayR.add(myData)\n          });\n    if (allLeagues.others.s != null)\n      allCountriesS.forEach((element) => {\n            myFilterData = allLeagues.others.s.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayS.add(myData)\n          });\n    if (allLeagues.others.ss != null)\n      allCountriesSS.forEach((element) => {\n            myFilterData = allLeagues.others.ss.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArraySS.add(myData)\n          });\n    if (allLeagues.others.t != null)\n      allCountriesT.forEach((element) => {\n            myFilterData = allLeagues.others.t.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayT.add(myData)\n          });\n    if (allLeagues.others.u != null)\n      allCountriesU.forEach((element) => {\n            myFilterData = allLeagues.others.u.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayU.add(myData)\n          });\n    if (allLeagues.others.uu != null)\n      allCountriesUU.forEach((element) => {\n            myFilterData = allLeagues.others.uu.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayUU.add(myData)\n          });\n    if (allLeagues.others.v != null)\n      allCountriesV.forEach((element) => {\n            myFilterData = allLeagues.others.v.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayV.add(myData)\n          });\n    if (allLeagues.others.y != null)\n      allCountriesY.forEach((element) => {\n            myFilterData = allLeagues.others.y.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayY.add(myData)\n          });\n    if (allLeagues.others.z != null)\n      allCountriesZ.forEach((element) => {\n            myFilterData = allLeagues.others.z.where((x) {\n              return x.category.name == element;\n            }),\n            myData = LeagueTempModel(\n                country: element, leagues: myFilterData.toList()),\n            myArrayZ.add(myData)\n         

Ter*_*iel 6

从错误来看,当小部件仍在构建或忙于构建时,您似乎正在调用 setState (本质上是重建)。

我过去也遇到过类似的问题。一种解决方法是仅将导致错误的构建延迟几毫秒,以便当前构建可以完成。我猜这是在update()方法中发生的。因此只需将update()替换为以下内容即可。

Future.delayed(Duration(milliseconds: 20),(){update();});
Run Code Online (Sandbox Code Playgroud)

另外,也许只是一个旁注,因为您没有发布完整的示例,请确保不要在构建方法内部调用 getLeague() ,因为这基本上最终会成为无限循环。重建并刷新数据。而是将其放在有状态小部件的 onInit 方法中。