如何返回并刷新Flutter中的上一页? [英] How to go back and refresh the previous page in Flutter?

查看:2270
本文介绍了如何返回并刷新Flutter中的上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主页,当单击该主页后,我将通过导航进入另一个页面,进行一些操作,然后按返回按钮,这会将我带回到该主页.但是问题是主页没有刷新.

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?

推荐答案

当您导航回首页时,可以触发API调用,例如伪代码

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

或者,如果API经常更新,您也可以使用流,新数据将在您的ListView

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

例如使用firebase,我们可以做到

For example with firebase we can do this

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

只要您更改数据库中的某些内容(例如,从编辑个人资料"页面进行更改),该信息就会反映在您的个人资料页面上.在这种情况下,这仅是可能的,因为我正在使用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.

这篇关于如何返回并刷新Flutter中的上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆