弹出屏幕后如何调用函数更新值? [英] How to call function to update value after poping screen in flutter?
问题描述
屏幕1:显示带有添加按钮的项目列表屏幕2:将新项目添加到列表的表单.
Screen 1: shows list of item with add button Screen 2: form to add new item to list.
屏幕2》屏幕1在屏幕2中调用navigator.pop()时,如何在屏幕1中调用方法/设置状态(以更新列表)?谁能帮我吗?
Screen 2 》 Screen 1 While calling navigator.pop() in screen 2, how to call method/setstate (to update list) in screen 1? Can anyone help me out?
我不想重新启动屏幕.我只需要运行方法以在弹出上一个屏幕后更新列表?
I didn't want to relaunch screen again.I just need to run method to update my list after popping previous screen?
推荐答案
从屏幕1导航到屏幕2时,使用 push
方法. push
方法返回一个 Future
对象.在将屏幕2从导航堆栈中弹出后,可以使用 Future
的 then
方法执行代码.
When you navigate from Screen 1 to Screen 2, you use the push
method. The push
method returns a Future
object.
You can use the then
method of Future
to execute your code after Screen 2 was popped from the navigation stack.
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => Screen2(),
))
.then((value) {
// you can do what you need here
// setState etc.
});
这篇关于弹出屏幕后如何调用函数更新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!