小编典典

Flutter Navigator.of(context).pop与Navigator.pop(context)的区别

flutter

Navigator.of(context).pop和之间有什么区别Navigator.pop(context)

在我看来,两者似乎都做同样的工作,实际的区别是什么。被弃用了吗?


阅读 4397

收藏
2020-08-13

共1个答案

小编典典

Navigator.push(上下文,路由)vs Navigator.of(上下文).push(路由)

导航器用于管理应用程序的页面堆栈(路线)。将给定的路线推送到屏幕上(导航器)时,我们需要获取正确的导航器,然后进行推送。

Navigator.of(context).push(route)拆分.of(context)以获取正确的Navigator和.push(route)Navigator.of(context)具有可选参数,如果rootNavigator设置为true,则从最远的位置开始给出NavigatorState。

  static NavigatorState of(
    BuildContext context, {
    bool rootNavigator = false,
    bool nullOk = false,
  })

Navigator.push(context, route)是静态方法,并且两者都同时执行。内部调用Navigator.of(context).push(route)。导航器最紧密地包围了给定的上下文。

static Future<T> push<T extends Object>(BuildContext context, Route<T> route) {
    return Navigator.of(context).push(route);
}

pop()与相似push()

当多个导航器嵌套在App中时。showDialog(...)方法创建的对话框路由被推送到根导航器。如果应用程序具有多个Navigator对象,则可能有必要调用Navigator.of(context, rootNavigator: true).pop(result)来关闭对话框,而不仅仅是Navigator.pop(context, result)

2020-08-13