luc
luc

Reputation: 1539

FLUTTER: Unhandled Exception: setState() or markNeedsBuild() called during build

I have a problem with this exception and I am stuck, I need your help. I use navigator inside of a streambuilder, I already use it inside of streambuilder but this time I have an exception and I don't know how to solve it. It try to add dispose function but I don't know how to close a streambuilder.

this is my state Widget build(BuildContext context) { changestate(); return Scaffold( backgroundColor: Theme.of(context).backgroundColor, body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ SizedBox(height: 40), Text('VOUS', style: TextStyle(fontSize: 50)), Text( 'devez ' + legage, style: TextStyle(fontSize: 35), ), SizedBox(height: 30), Image(image: AssetImage('assets/images/hands/2htop.png')), SizedBox(height: 30), Expanded( child: StreamBuilder( stream: Firestore.instance .collection('rooms') .document(pincode) .snapshots(), builder: (context, snapshot) { if (snapshot.hasData && snapshot.data['nb screen'] == 1) { gotdata++; newduel(context); } return StreamBuilder<QuerySnapshot>( stream: Firestore.instance .collection('rooms') .document(pincode) .collection('users') .snapshots(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (!snapshot.hasData) return Text("Chargement...."); else { return new GridView.count( crossAxisCount: 5, children: snapshot.data.documents .map((DocumentSnapshot document) { return Container( child: OvalPic( document['photo'], document['couleur']), ); }).toList()); } }, ); }, ), ), Container( child: (ffull2 == 0) ? AnimatedLiquidLinearProgressIndicator() : fgbutton(mypink, 'DUEL SUIVANT', context)), SizedBox(height: 30) ], ), ), ); }

void changestate() {
if (dejareload == 0) {
  dejareload++;
  Future.delayed(
      const Duration(seconds: 3),
      () => setState(() {
            ffull2 = 1;
          }));
}

}

and this is what is in newduel:

Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => Votes()));

ERROR IS:

enter image description here

Upvotes: 0

Views: 1466

Answers (1)

Murat Aslan
Murat Aslan

Reputation: 1580

Wrap your setState method with a mounted condition like below.

the problem is, I think you made an API call somewhere then you dispose the page and the data arrives at the page when its already disposed, or any delayed actions.

void changestate() {
if (dejareload == 0) {
  dejareload++;
   if(mounted){
      Future.delayed(
      const Duration(seconds: 3),
      () => setState(() {
            ffull2 = 1;
      }));
   }

}

Upvotes: 1

Related Questions