Reputation: 53
i was trying to get data from json , i got some errors but i dont know why.
i got this error :
E/flutter ( 4560): [ERROR:flutter/lib/ui/ui_dart_state.cc(207)] Unhandled Exception: setState() or markNeedsBuild() called during build.
E/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.
E/flutter ( 4560): The widget on which setState() or markNeedsBuild() was called was:
E/flutter ( 4560): GetBuilder<GamesController>
E/flutter ( 4560): The widget which was currently being built when the offending call was made was:
E/flutter ( 4560): _BodyBuilder
E/flutter ( 4560): #0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4305:11)
E/flutter ( 4560): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4320:6)
E/flutter ( 4560): #2 State.setState (package:flutter/src/widgets/framework.dart:1108:15)
and my code :
Future getLeagues(var sportCategory) async {
//update();
myArrayA.clear();
myArrayB.clear();
myArrayC.clear();
myArrayCC.clear();
myArrayD.clear();
myArrayE.clear();
myArrayF.clear();
myArrayG.clear();
myArrayGG.clear();
myArrayH.clear();
myArrayII.clear();
myArrayI.clear();
myArrayJ.clear();
myArrayK.clear();
myArrayL.clear();
myArrayM.clear();
myArrayN.clear();
myArrayO.clear();
myArrayOO.clear();
myArrayP.clear();
myArrayR.clear();
myArrayS.clear();
myArraySS.clear();
myArrayT.clear();
myArrayU.clear();
myArrayUU.clear();
myArrayV.clear();
myArrayY.clear();
myArrayZ.clear();
leagueList.clear();
leagueEnum = LeagueEnum.Loading;
update();
_apiServices.getLeagues(sportCategory).then((value) {
if (value != false) {
allLeagues = LeagueWCountryModel.fromJson(value);
//A
if (allLeagues.others.a != null)
allLeagues.others.a.forEach((element) {
allCountriesA.contains(element.category.name)
? null
: allCountriesA.add(element.category.name);
});
//B
if (allLeagues.others.b != null)
allLeagues.others.b.forEach((element) {
allCountriesB.contains(element.category.name)
? null
: allCountriesB.add(element.category.name);
});
//C
if (allLeagues.others.c != null)
allLeagues.others.c.forEach((element) {
allCountriesC.contains(element.category.name)
? null
: allCountriesC.add(element.category.name);
});
//Ç
if (allLeagues.others.cc != null)
allLeagues.others.cc.forEach((element) {
allCountriesCC.contains(element.category.name)
? null
: allCountriesCC.add(element.category.name);
});
//D
if (allLeagues.others.d != null)
allLeagues.others.d.forEach((element) {
allCountriesD.contains(element.category.name)
? null
: allCountriesD.add(element.category.name);
});
//E
if (allLeagues.others.e != null)
allLeagues.others.e.forEach((element) {
allCountriesE.contains(element.category.name)
? null
: allCountriesE.add(element.category.name);
});
//F
if (allLeagues.others.f != null)
allLeagues.others.f.forEach((element) {
allCountriesF.contains(element.category.name)
? null
: allCountriesF.add(element.category.name);
});
//G
if (allLeagues.others.g != null)
allLeagues.others.g.forEach((element) {
allCountriesG.contains(element.category.name)
? null
: allCountriesG.add(element.category.name);
});
//GG
if (allLeagues.others.gg != null) if (allLeagues.others.gg != null)
allLeagues.others.gg.forEach((element) {
allCountriesGG.contains(element.category.name)
? null
: allCountriesGG.add(element.category.name);
});
//H
if (allLeagues.others.h != null)
allLeagues.others.h.forEach((element) {
allCountriesH.contains(element.category.name)
? null
: allCountriesH.add(element.category.name);
});
//II
if (allLeagues.others.ii != null)
allLeagues.others.ii.forEach((element) {
allCountriesII.contains(element.category.name)
? null
: allCountriesII.add(element.category.name);
});
//İ
if (allLeagues.others.i != null)
allLeagues.others.i.forEach((element) {
allCountriesI.contains(element.category.name)
? null
: allCountriesI.add(element.category.name);
});
//J
if (allLeagues.others.j != null)
allLeagues.others.j.forEach((element) {
allCountriesJ.contains(element.category.name)
? null
: allCountriesJ.add(element.category.name);
});
//K
if (allLeagues.others.k != null)
allLeagues.others.k.forEach((element) {
allCountriesK.contains(element.category.name)
? null
: allCountriesK.add(element.category.name);
});
//L
if (allLeagues.others.l != null)
allLeagues.others.l.forEach((element) {
allCountriesL.contains(element.category.name)
? null
: allCountriesL.add(element.category.name);
});
//İ
if (allLeagues.others.m != null)
allLeagues.others.m.forEach((element) {
allCountriesM.contains(element.category.name)
? null
: allCountriesM.add(element.category.name);
});
//N
if (allLeagues.others.n != null)
allLeagues.others.n.forEach((element) {
allCountriesN.contains(element.category.name)
? null
: allCountriesN.add(element.category.name);
});
//O
if (allLeagues.others.o != null) if (allLeagues.others.o != null)
allLeagues.others.o.forEach((element) {
allCountriesO.contains(element.category.name)
? null
: allCountriesO.add(element.category.name);
});
//OO
if (allLeagues.others.oo != null)
allLeagues.others.oo.forEach((element) {
allCountriesOO.contains(element.category.name)
? null
: allCountriesOO.add(element.category.name);
});
//P
if (allLeagues.others.p != null)
allLeagues.others.p.forEach((element) {
allCountriesP.contains(element.category.name)
? null
: allCountriesP.add(element.category.name);
});
//R
if (allLeagues.others.r != null)
allLeagues.others.r.forEach((element) {
allCountriesR.contains(element.category.name)
? null
: allCountriesR.add(element.category.name);
});
//S
if (allLeagues.others.s != null)
allLeagues.others.s.forEach((element) {
allCountriesS.contains(element.category.name)
? null
: allCountriesS.add(element.category.name);
});
//SS
if (allLeagues.others.ss != null) if (allLeagues.others.ss != null)
allLeagues.others.ss.forEach((element) {
allCountriesSS.contains(element.category.name)
? null
: allCountriesSS.add(element.category.name);
});
//T
if (allLeagues.others.t != null)
allLeagues.others.t.forEach((element) {
allCountriesT.contains(element.category.name)
? null
: allCountriesT.add(element.category.name);
});
//U
if (allLeagues.others.u != null)
allLeagues.others.u.forEach((element) {
allCountriesU.contains(element.category.name)
? null
: allCountriesU.add(element.category.name);
});
//Ü
if (allLeagues.others.uu != null) if (allLeagues.others.uu != null)
allLeagues.others.uu.forEach((element) {
allCountriesUU.contains(element.category.name)
? null
: allCountriesUU.add(element.category.name);
}); //V
if (allLeagues.others.v != null)
allLeagues.others.v.forEach((element) {
allCountriesV.contains(element.category.name)
? null
: allCountriesV.add(element.category.name);
}); //Y
if (allLeagues.others.y != null)
allLeagues.others.y.forEach((element) {
allCountriesY.contains(element.category.name)
? null
: allCountriesY.add(element.category.name);
}); //İ
if (allLeagues.others.z != null)
allLeagues.others.z.forEach((element) {
allCountriesZ.contains(element.category.name)
? null
: allCountriesZ.add(element.category.name);
});
//---
if (allLeagues.others.a != null)
allCountriesA.forEach((element) => {
myFilterData = allLeagues.others.a.where((Top x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayA.add(myData)
});
if (allLeagues.others.b != null)
allCountriesB.forEach((element) => {
myFilterData = allLeagues.others.b.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayB.add(myData)
});
if (allLeagues.others.c != null)
allCountriesC.forEach((element) => {
myFilterData = allLeagues.others.c.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayC.add(myData)
});
if (allLeagues.others.cc != null)
allCountriesCC.forEach((element) => {
myFilterData = allLeagues.others.cc.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayC.add(myData)
});
if (allLeagues.others.d != null)
allCountriesD.forEach((element) => {
myFilterData = allLeagues.others.d.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayD.add(myData)
});
if (allLeagues.others.e != null)
allCountriesE.forEach((element) => {
myFilterData = allLeagues.others.e.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayE.add(myData)
});
if (allLeagues.others.f != null)
allCountriesF.forEach((element) => {
myFilterData = allLeagues.others.f.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayF.add(myData)
});
if (allLeagues.others.g != null)
allCountriesG.forEach((element) => {
myFilterData = allLeagues.others.g.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayG.add(myData)
});
if (allLeagues.others.gg != null)
allCountriesGG.forEach((element) => {
myFilterData = allLeagues.others.gg.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayGG.add(myData)
});
if (allLeagues.others.h != null)
allCountriesH.forEach((element) => {
myFilterData = allLeagues.others.h.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayH.add(myData)
});
if (allLeagues.others.ii != null) if (allLeagues.others.ii != null)
allCountriesII.forEach((element) => {
myFilterData = allLeagues.others.ii.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayII.add(myData)
});
if (allLeagues.others.i != null)
allCountriesI.forEach((element) => {
myFilterData = allLeagues.others.i.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayI.add(myData)
});
if (allLeagues.others.j != null)
allCountriesJ.forEach((element) => {
myFilterData = allLeagues.others.j.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayJ.add(myData)
});
if (allLeagues.others.k != null)
allCountriesK.forEach((element) => {
myFilterData = allLeagues.others.k.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayK.add(myData)
});
if (allLeagues.others.l != null)
allCountriesL.forEach((element) => {
myFilterData = allLeagues.others.l.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayL.add(myData)
});
if (allLeagues.others.m != null)
allCountriesM.forEach((element) => {
myFilterData = allLeagues.others.m.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayM.add(myData)
});
if (allLeagues.others.n != null)
allCountriesN.forEach((element) => {
myFilterData = allLeagues.others.n.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayN.add(myData)
});
if (allLeagues.others.o != null)
allCountriesO.forEach((element) => {
myFilterData = allLeagues.others.o.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayO.add(myData)
});
if (allLeagues.others.oo != null)
allCountriesOO.forEach((element) => {
myFilterData = allLeagues.others.oo.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayOO.add(myData)
});
if (allLeagues.others.p != null)
allCountriesP.forEach((element) => {
myFilterData = allLeagues.others.p.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayP.add(myData)
});
if (allLeagues.others.r != null)
allCountriesR.forEach((element) => {
myFilterData = allLeagues.others.r.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayR.add(myData)
});
if (allLeagues.others.s != null)
allCountriesS.forEach((element) => {
myFilterData = allLeagues.others.s.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayS.add(myData)
});
if (allLeagues.others.ss != null)
allCountriesSS.forEach((element) => {
myFilterData = allLeagues.others.ss.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArraySS.add(myData)
});
if (allLeagues.others.t != null)
allCountriesT.forEach((element) => {
myFilterData = allLeagues.others.t.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayT.add(myData)
});
if (allLeagues.others.u != null)
allCountriesU.forEach((element) => {
myFilterData = allLeagues.others.u.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayU.add(myData)
});
if (allLeagues.others.uu != null)
allCountriesUU.forEach((element) => {
myFilterData = allLeagues.others.uu.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayUU.add(myData)
});
if (allLeagues.others.v != null)
allCountriesV.forEach((element) => {
myFilterData = allLeagues.others.v.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayV.add(myData)
});
if (allLeagues.others.y != null)
allCountriesY.forEach((element) => {
myFilterData = allLeagues.others.y.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayY.add(myData)
});
if (allLeagues.others.z != null)
allCountriesZ.forEach((element) => {
myFilterData = allLeagues.others.z.where((x) {
return x.category.name == element;
}),
myData = LeagueTempModel(
country: element, leagues: myFilterData.toList()),
myArrayZ.add(myData)
});
leagueEnum = LeagueEnum.Loaded;
update();
} else {
print('Ligler çekilirken apide bir sorun oluştu..');
leagueEnum = LeagueEnum.Error;
update();
}
});
in real devices, error 1 happened, in simulator , error 2 happened. sometimes i only see loading screen, i dont understand why.
i dont understand why this happened, how can i solve this problem ? help me, please. thank you.
Upvotes: 2
Views: 1258
Reputation: 1475
From the error, it seems that you are calling setState (which essentially is a rebuild) when the widget is either still building or busy building.
I have come across similar issues in the past. One workaround is to just delay your build causing the error by a few milliseconds so the current build can finish. Which I am guessing is happening in the update() method. So just replace update() with the below.
Future.delayed(Duration(milliseconds: 20),(){update();});
Also maybe just a side note since you did not post the full example, be sure to not call getLeague() inside of the build method, because that would basically just end up being an endless loop. Rebuilding and also refreshing the data. Rather place it in the onInit method of your stateful widget.
Upvotes: 5