Shariar Saimon
Shariar Saimon

Reputation: 943

How to return from a widget to StreamBuilder after navigating to another page in flutter app?

So the first picture has codes from the first page where I have a StreamBuilder in my flutter app, there I tried navigating to the stateful widget named PickupScreen containing the code in second picture and it gave me an error which says SetState( or markNeedsbuild() called during build.So I called the widget directly and it worked but the problem is I cannot go the previous page I know I can't use pop but I need a solution for returning to previous page and show the else code.Currently I am Navigating to another page named HomePage.enter image description here

enter image description here

Upvotes: 0

Views: 1688

Answers (1)

Miguel Ruivo
Miguel Ruivo

Reputation: 17756

You can't do it like that. Either you manage your screen state based on your widget.channel.stream or you must design your UI in a different way so you can actually push a new screen on your screen 1 and then you will be able to pop.

If you still want to keep it like that, you can do something like the following in your first screen:

return StreamBuilder(
  stream: widget.channel.stream,
  builder: (context, snapshot){
   if(snapshot.hasData && jsonDecode(snapshot.data['ntype'] == 10){
      WidgetsBinding.instance.addPostFrameCallback((_) => Navigator.push(context, MaterialPageRoute(builder: (_) => PickupScreen(data: jsonDecode(snapshot.data))));
    return const SizedBox();
 } else {
    return  _isLoading ? LoadingScreen() : Scaffold();
  }
});

Also, next time, make sure you copy/paste code instead of screenshots to make it easy to edit with modifications. :)

Upvotes: 2

Related Questions