is it possible to refresh or reload page with navigator.pop... like 1st (nav.push=>2) page to 2nd page and back from 2nd (nav.pop, value) to 1st page?

NilxSingh picture NilxSingh · Apr 20, 2019 · Viewed 10.3k times · Source

I want to pass values from 2nd page to 1st page with navigator.pop and refresh or reload my 1st page with new values in initstate or any other work around?

I am able to get these values in 1st page but not able to refresh or reload the page with new values in initstate where i want to pass new data in API n get response...

any help?

I am redirecting to list page from this...

getBrand(BuildContext context) async {
    tempBrand = await Navigator.push(context,
        MaterialPageRoute(builder: (context) => BrandList(widget.subCatID)));
    selectedBrand = tempBrand.name;
    selectedBrandID = tempBrand.id;
  }

now here i am passing those values in navigator.pop

Getting value here and passing back to 1st page.

Container(
          padding: EdgeInsets.all(10),
          child: ListView.separated(
              separatorBuilder: (context, index) =>
                  Divider(color: Color(0xffcccccc)),
              itemCount: prodBrands.length,
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  child: InkWell(
                    onTap: () {
                      tempProdBrand = ProdBrandListModel.Message(
                        id: prodBrands[index].id,
                        name: prodBrands[index].name,
                      );

                      Navigator.pop(context, tempProdBrand);

                    },
                    child: Container(
                      child: Text(prodBrands[index].name),
                      padding: EdgeInsets.all(10.0),
                    ),
                  ),
                );
              }),
        )

Want to use new values here...

submitProduct() {
    api
        .newbiz(
      selectedBrandID,
    )
        .then((response) {
      setState(() {
        productID = response.message;
      });
    });
    setState(() {});
  }

Simply want to refresh my page from initstate or any other way when i pop back to 1st page with my new values passed in pop function.

Answer

Ridha Rezzag picture Ridha Rezzag · May 24, 2020

to navigate from Page1 to Page2

usually you use Navigator.push() for example:

// Page1
    Navigator.push(context, MaterialPageRoute(builder: (context) => Page2()));

and when you are in Page2 you do some work for example saving some data in shared preference and go back to Page1 and refresh Page1 to get the new data from shared preferences, what you can do is to pop to Page1 using

//Page2
    Navigator.pop(context);

this is not gonna be enough to refresh your Page1 so you need to attach a .then in the Page1 Navigator as a callback it will be called when you pop from page2 and that .then should have a set state to trigger a rebuild just call a something inside the set state to cause a rebuild like this:

//Page1
    Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) {
                  setState(() {
                    // refresh state of Page1
                  });
                });