How to call Navigator.push in FutureBuilder

3,796 views
Skip to first unread message

rafael....@gmail.com

unread,
Jul 9, 2018, 10:17:49 AM7/9/18
to Flutter Dev

I want to redirect to another page after the future is resolved, this is a code sample of what I want

  1. FutureBuilder(
  2. future: _bloc.formResponse,
  3. builder: (context,AsyncSnapshot<Result> snapshot){
  4. if(snapshot.hasData){
  5. Navigator.pushNamed(context, "/home");
  6. }
  7. return _buildSignUpPage();
  8. }
  9. )

but the previous code returns an exception

  1. I/flutter (29158): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════════════════════════════════════════════
  2. I/flutter (29158): The following assertion was thrown building FutureBuilder<Result>(dirty, state:
  3. I/flutter (29158): _FutureBuilderState<Result>#de36b):
  4. I/flutter (29158): setState() or markNeedsBuild() called during build.

Mikkel Ravn

unread,
Jul 9, 2018, 10:28:54 AM7/9/18
to rafael....@gmail.com, Flutter Dev
FutureBuilder is a widget that builds and rebuilds its subtree based on interaction with a Future. What you want is just to call a function upon completion of the Future. For that just use _bloc.formResponse.then((_) => Navigator.pushNamed(...));

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Mikkel Nygaard Ravn
Software Engineer

Ramit Dour

unread,
Apr 16, 2020, 7:55:53 PM4/16/20
to Flutter Development (flutter-dev)
Hi Rafel , I also trying something similar, did you got a solution ? if so kindly share it, thanks in advance. 
Reply all
Reply to author
Forward
0 new messages