Leandro Gamarra
Leandro Gamarra

Reputation: 151

Call a future builder again

Im stuck in how I can manage to call a future Builder again on a button press to repeat the process indefinitely until it goes OK.

Here is my code:

Widget build(BuildContext context) 
  {
    return FutureBuilder( future: sincroProcess(),
                          builder: (BuildContext context, AsyncSnapshot snapshot) 
                                    {
                                      if (snapshot.hasData) 
                                      {
                                        Map sincroProcessResponse = snapshot.data;
                                        
                                        if( sincroProcessResponse['allok'] )
                                        {
                                          return ListBody(children: [ OutlinedButton
                                                                      ( 
                                                                        child: Text("Continue"),
                                                                        onPressed: goInit(),
                                                                      ),  
                                                                      Text( sincroProcessResponse['msg'] ) 
                                                                    ]);
                                        }
                                        else
                                        {
                                          return ListBody(children: [ OutlinedButton
                                                                      ( 
                                                                        child: Text("Try again"),
                                                                        onPressed: sincroProcess(), //need this to re-run
                                                                      ),  
                                                                      Text( sincroProcessResponse['msg'] ) 
                                                                    ]);
                                        }
                                      }
                                      else
                                      {
                                        return ListBody(children: [ CircularProgressIndicator(backgroundColor: Colors.grey), 
                                                                    Text("We are preparing everything for your first usage =)...") ]);
                                      }
                                    });
  }
}

I don't know if I am being clear enough, the behavior of the page should be: show circular progress while running the sincroProcess, if the response its ok, show a button to go to another page, if not, show a button to re-run the sincroprocess with the circular progress indicator. I cant imagine how to re-use my code!

Upvotes: 0

Views: 1318

Answers (1)

Ibrahim Ali
Ibrahim Ali

Reputation: 2503

Make your widget a Statefull widget and call setState() to rebuild the widget

...
else {
  return ListBody(children: [
    OutlinedButton(
      child: Text("Try again"),
      onPressed: (){setState((){});}, //need this to re-run // call setState
    ),
    Text(sincroProcessResponse['msg'])
  ]);
}

Upvotes: 2

Related Questions