首页 文章

Navigator.push()的类型无法签名到onPressed

提问于
浏览
0

我正在浏览颤动的文档和示例,我陷入了路由 . 这里是文档https://docs.flutter.io/flutter/widgets/Navigator-class.html它告诉我可以用一个带有 Navigator 导航功能的按钮

onPressed: (context) {
  Navigator.pop(context);
}

但我的ide(vs代码)和调试器认为不可能 . 当我试图做它时,它显示我的错误 .

[dart] The argument type '(dynamic) → Null' can't be assigned to the parameter type '() → void'.

构建也会因异常而失败

setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framewo…

不太清楚这是什么意思......有什么建议吗?

2 回答

  • 1

    不要将 context 传递给 onPressed ,因为它是一个无效回调,所以它不会返回任何内容 . 仅用它来执行逻辑 .

  • 1

    如果回调是在 context 可用的范围内创建的,则无需将 context 传递给 onPressed 回调, context 在回调中可用:

    build(BuildContext context) {
      ... 
      onPressed: () {
        Navigator.pop(context);
      }
    }
    

相关问题