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?

给你一囗甜甜゛ 提交于 2020-08-08 04:27:29

问题


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?

1. 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...

2. 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),
                    ),
                  ),
                );
              }),
        )

3. 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.


回答1:


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
                  });
                });


来源:https://stackoverflow.com/questions/55769939/is-it-possible-to-refresh-or-reload-page-with-navigator-pop-like-1st-nav-pus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!