Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 获取颤振中方法的buildContext-作为参数或使用globalKey';当前上下文_Flutter_Dart_Flutter Layout - Fatal编程技术网

Flutter 获取颤振中方法的buildContext-作为参数或使用globalKey';当前上下文

Flutter 获取颤振中方法的buildContext-作为参数或使用globalKey';当前上下文,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我有一些需要构建上下文的方法,例如弹出当前页面、显示modals、隐藏snackBar 我不确定哪种方法是获得buildContext的更好的解决方案: 创建脚手架状态的全局键并调用全局键的currentContext 将buildContext从build方法一直传递到当前方法 这两种方式有区别吗?或者,如果它们同样适用于这种情况,那么其中一种方式比另一种方式更受欢迎还是更符合惯例 谢谢我认为在大多数情况下使用全局密钥被认为是不好的做法。我认为当使用导航器、主题和媒体查询时,将构建上下文作为参

我有一些需要构建上下文的方法,例如弹出当前页面、显示modals、隐藏snackBar

我不确定哪种方法是获得buildContext的更好的解决方案:

  • 创建脚手架状态的全局键并调用全局键的currentContext
  • 将buildContext从build方法一直传递到当前方法
  • 这两种方式有区别吗?或者,如果它们同样适用于这种情况,那么其中一种方式比另一种方式更受欢迎还是更符合惯例


    谢谢

    我认为在大多数情况下使用全局密钥被认为是不好的做法。我认为当使用
    导航器
    主题
    媒体查询
    时,将
    构建上下文
    作为参数的函数是常见的。但要记住,最好是练习