【问题标题】:Using the Navigator class with no buttons (i.e. onPressed...) in a void method在 void 方法中使用没有按钮的 Navigator 类(即 onPressed...)
【发布时间】:2021-07-31 15:22:20
【问题描述】:

我有一个基于密码条件调用的方法:

void passwordLogic() {
  if(equals(password,correctPassword)){
      //// I want to switch to another screen here.
        Navigator.push(context, MaterialPageRoute(builder: (context) {
        return Main();
      }));
  }

但是“push”之后的上下文会报错:undefined name 'context'。

【问题讨论】:

    标签: flutter navigator


    【解决方案1】:

    您必须将上下文作为函数的参数,即void passwordLogic(BuildContext context)

    更正的代码:

    void passwordLogic(BuildContext context) {
      if(equals(password,correctPassword)){
          //// I want to switch to another screen here.
            Navigator.push(context, MaterialPageRoute(builder: (context) {
            return Main();
          }));
      }
    

    【讨论】:

    • 感谢您的澄清!现在可以使用了。
    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 2021-03-28
    • 2020-05-08
    • 2020-04-02
    • 2020-01-16
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多