【问题标题】:Navigating to the updated page in flutter在颤振中导航到更新的页面
【发布时间】:2021-08-07 14:40:40
【问题描述】:

当我从其他页面插入数据并弹出此页面以返回列表页面时,我必须刷新列表页面以获取更新的数据列表。我想在不按刷新按钮的情况下返回页面的更新版本。我正在使用 pushNamed,并且我希望在输入页面上有一个后退箭头按钮。

【问题讨论】:

  • 从页面获取数据后,执行 setState 以使用最新数据更新 ui

标签: flutter flutter-navigation


【解决方案1】:

使用Navigator.pushNavigator.pushNamed 会向您返回一个Future 对象。 当您在第二个屏幕中使用 Navigator.pop 时,此 Future 会解析。

这是一个基本示例。假设你有调用函数goToNewPage 来推送一个新屏幕,你可以这样做。

class Screen1State extends State<Screen1> {

  void goToNewPage (context) {
    Navigator.of(context).pushNamed('screen2').then((_) {
       // The screen2 has popped. so you can just call setState here to just rebuild
       // You can do anything you want here, that needs to happen after the new page has been popped. 
       setState(() {});
    });
  }   

  ... rest of your code.

注意,您应该在callback 中执行的操作的实际实现取决于您的小部件如何获取数据。但是,在 callback 中弹出新页面后,您可以做任何您想做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2020-07-14
    • 2021-11-24
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多