弹出第二个页面后如何刷新主页?
How do I refresh my main page after popping my second page?
我的主页由 FutureBuilder 生成的 ListView 组成。第二页由第一页的一个FAB打开,用于向第一页的ListView添加元素。我的问题是,在自动弹出第二页以显示添加的元素后,我找不到刷新第一页的方法。
我搜索过其他有类似问题的话题,但没有明确的答案。
您可以做的是等待下一页的弹出,一旦发生,您可以触发一个函数来重新加载您的数据。
创建一个名为 pushRoute 的函数,它会在导航器堆栈上推送一条路线,并在您想要导航时使用此函数。
由于这是一个异步函数,您可以在它的末尾链接一个 .then() 。无论你传递给 .then() 什么都会在第二页上点击 Navigator.pop() 之后发生。
pushRoute() async {
await Navigator.push(...);
}
...
...
//when you want to navigate to page 2, use pushRoute().
//inside the onTap of your FAB
onTap() {
pushRoute().then(() { //this .then() will be triggered when the Navigator.pop() is hit on the second page.
loadData();
}); //put all your data loading code in this function. (You can use the same loadData in the initState to load the very first time.)
}
我的主页由 FutureBuilder 生成的 ListView 组成。第二页由第一页的一个FAB打开,用于向第一页的ListView添加元素。我的问题是,在自动弹出第二页以显示添加的元素后,我找不到刷新第一页的方法。
我搜索过其他有类似问题的话题,但没有明确的答案。
您可以做的是等待下一页的弹出,一旦发生,您可以触发一个函数来重新加载您的数据。
创建一个名为 pushRoute 的函数,它会在导航器堆栈上推送一条路线,并在您想要导航时使用此函数。
由于这是一个异步函数,您可以在它的末尾链接一个 .then() 。无论你传递给 .then() 什么都会在第二页上点击 Navigator.pop() 之后发生。
pushRoute() async {
await Navigator.push(...);
}
...
...
//when you want to navigate to page 2, use pushRoute().
//inside the onTap of your FAB
onTap() {
pushRoute().then(() { //this .then() will be triggered when the Navigator.pop() is hit on the second page.
loadData();
}); //put all your data loading code in this function. (You can use the same loadData in the initState to load the very first time.)
}