2562

Trigger FirstPage's setState method after popping Second page

Question:

I have two pages in myapp namely FirstPage and SecondPage.

Basically, FirstPage widget displays a ListView with a list of items while SecondPage widget is where I can add/delete items to the list.

I can navigate to the SecondPage by:

Navigator.of(context).pushNamed("/SecondPage");

And also I can go back to the FirstPage by using:

Navigator.of(context).pop();

My problem is I can't figure out how am I going to trigger the setState method of FirstPage widget after popping the SecondPage so that the FirstPage's ListView is updated.

I would appreciate any hint.

Answer1:

Whenever we <strong>push</strong>, we will get a <strong>future</strong>, we can use that future to trigger the setState

Future pushNamed = Navigator.of(context).pushNamed("/SecondPage"); pushNamed.then((_)=> setState(() {}));

Refer <a href="https://flutter.io/cookbook/navigation/returning-data/" rel="nofollow">here</a> to send data from secondScreen to firstScreen.

Answer2:

Use this code to send values from child View to your parent view

Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text(widget.title), ), body: new Center( child: new FlatButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => SecondView(callBackValue)), ); }, child: new Text('Open Child Widget'))), ); } //Your Callback Values will be received here void callBackValue(double value) { print('Value $value'); } // Your SecondPage class SecondView extends StatefulWidget{ final void Function(double data) callback; SecondView(this.callback); @override _SecondView createState() => new _SecondView(); } class _SecondView extends State<SecondView>{ @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Child Widget'), ), body: new Center( child: new FlatButton(onPressed: (){ widget.callback(10.0); }, child: new Text('Send Value to parent')), ), ); } }

Answer3:

what you trying to achieve is similar to the startActivityForResult() method in Android.

This can be achieved in flutter something like this :-

in your first activity

Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView()); // now it will wait for the secondView to return some data //and the below code will be executed whenever the second view pops. setState((){});

Hope this was helpful.

And dont forget to write async in your method declaration for the navigation.

void _navigateToSecondActivity async { Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView()); setState((){}); }

Just call this function when you need to navigate to the second activity.

or on your onTap()

onTap: () async{ Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView()); setState((){}); }

Recommend

  • Unable to start activity ComponentInfo You need to use a Theme.AppCompat theme with this activity.
  • Copying rows in a database when rows have children
  • Passing information to server-side function in a Google Docs Add On
  • Why doesnt this Java loop in a thread work?
  • Diff between two dataframes in pandas
  • Force show.bind execution
  • Refering to the class itself from within a class mehod in Objective C
  • Disable Enter in editText android
  • Xamarin Forms - UWP Fonts
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Convert array of 8 bytes to signed long in C++
  • Cannot Parse HTML Data Using Android / JSOUP
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?