Flutter中如何返回刷新上一页? [英] How to go back and refresh the previous page in 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屋!