How to go back and refresh the previous page in Flutter?

46,270

Solution 1

You can trigger the API call when you navigate back to the first page like this pseudo-code

class PageOne extends StatefulWidget {
  @override
  _PageOneState createState() => new _PageOneState();
}

class _PageOneState extends State<PageOne> {
  _getRequests()async{

  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new RaisedButton(onPressed: ()=>
        Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>new PageTwo()),)
        .then((val)=>val?_getRequests():null),
      ),
    ));
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //somewhere
    Navigator.pop(context,true);
  }
}

Or you can just use a stream if the API is frequently updated, the new data will be automatically updated inside your ListView

For example with firebase we can do this

stream: FirebaseDatabase.instance.reference().child(
      "profiles").onValue

And anytime you change something in the database (from edit profile page for example), it will reflect on your profile page. In this case, this is only possible because I am using onValue which will keep listening for any changes and do the update on your behalf.

Solution 2

(In your 1st page): Use this code to navigate to the 2nd page.

Navigator.pushNamed(context, '/page2').then((_) {
  // This block runs when you have returned back to the 1st Page from 2nd.
  setState(() {
    // Call setState to refresh the page.
  });
});

(In your 2nd page): Use this code to return back to the 1st page.

Navigator.pop(context);

Solution 3

Simply i use this:

onPressed: () {
          Navigator.pop(context,
              MaterialPageRoute(builder: (context) => SecondPage()));
        },

this to close current page:

Navigator.pop

to navigate previous page:

MaterialPageRoute(builder: (context) => SecondPage())

In FirtsPage, me adding this for refresh on startUpPage:

 @override
  void initState() {
    //refresh the page here
    super.initState();
  }

Solution 4

If you are using GetX : use result when you navigate back from nextScreen as follow :

Get.back(result: 'hello');

and to reload previous page use this function :

void _navigateAndRefresh(BuildContext context) async {
    final result = await Get.to(()=>NextScreen());
    if(result != null){
      model.getEMR(''); // call your own function here to refresh screen
    }
  }

call this function instead of direct navigation to nextScreen

Share:
46,270
David
Author by

David

Updated on November 11, 2021

Comments

  • David
    David over 2 years

    I have a home page which when clicked takes me to another page through navigates, do some operations in then press the back button which takes me back to the home page. but the problem is the home page doesn't get refreshed.

    Is there a way to reload the page when i press the back button and refreshes the home page?