【问题标题】:Undefined Name 'context' in Navigator导航器中未定义的名称“上下文”
【发布时间】:2025-12-22 18:45:06
【问题描述】:

我是 Flutter 的初学者,前几天刚开始。 我想从一个小部件转到另一个文件中的另一个小部件。 但是当我使用导航器时,它会显示错误。

我尝试使用堆栈溢出类似问题的一些答案来解决它,但我无法解决它。 另外,我无法正确理解这些内容。

这些是其中的一些:

Undefined name 'context'

Undefined name 'context' in flutter navigation

“take_attendance.dart”

class TakeAttendance extends StatefulWidget {
  static String tag = 'take-attendance';
  @override
  _TakeAttendanceState createState() => _TakeAttendanceState();
}

bool colorCheck = false;

class _TakeAttendanceState extends State<TakeAttendance> {
  @override
  Widget build(BuildContext context) {
  }
}

“home_page.dart”

这是 home_page.dart 中的一个小部件。

Widget showPeriod(int a) {
  return Scaffold(
    appBar: new AppBar(
          title: new Text(
            "AppName",
            style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
          ),
          backgroundColor: Colors.blue[800],
          actions: <Widget>[
            new Padding(
                padding: EdgeInsets.only(right: 10.0),
                child: Icon(Icons.exit_to_app)),
          ],
        ),
        drawer: new AppDrawer(),
    body: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,

        children: <Widget>[
            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
              new Text("Period #$a",
            style: TextStyle(fontSize: 50.0,fontWeight: FontWeight.w400),
            )
            ],),

            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
              new Container(
                            child: Text(
                              "<time>",
                              style: TextStyle(color: Colors.white),
                            ),
                            decoration: new BoxDecoration(
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(30.0)),
                              color: Colors.grey,
                            ),
                            padding:
                                new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                          ),
                          SizedBox(width: 10.0),
                          new Container(
                            child: Text(
                              "<Class>",
                              style: TextStyle(color: Colors.white),
                            ),
                            decoration: new BoxDecoration(
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(30.0)),
                              color: Colors.grey,
                            ),
                            padding:
                                new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                          ),


            ],),
             new Padding(
                        padding: EdgeInsets.only(left: 10.0, top: 10.0),
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            new Container(
                              child: Text(
                                "An imaginary subject.",
                                style: TextStyle(color: Colors.white),
                              ),
                              decoration: new BoxDecoration(
                                borderRadius: new BorderRadius.all(
                                    new Radius.circular(30.0)),
                                color: Colors.grey,
                              ),
                              padding:
                                  new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                            ),
                          ],
                        )),

                        SizedBox(height: 40.0,),

                        new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            new RaisedButton(
                              child: const Text('GO!'),
                              color: Colors.redAccent,
                              elevation: 4.0,
                              splashColor: Colors.red,
                              onPressed: () {
                                // Perform some action
                                  Navigator.of(context).pushNamed(TakeAttendance.tag);
                                  //Navigator.push(context,takeAttendance());
                              },
                            ),

                            new RaisedButton(
                              child: const Text('Cancel'),
                              color: Colors.blueAccent,
                              elevation: 4.0,
                              splashColor: Colors.blue[200],
                              onPressed: () {
                                // Perform some action
                                //Navigator.pop(context);
                              },
                            ),
                          ],
                        )

          ]


        ),
  );
}

按 go 我希望它转到 take_attendance.dart 页面。

但是当使用导航器时,这是我得到的错误:

未定义的名称“上下文”。 尝试将名称更正为已定义的名称,或定义 name.dart(undefined_identifier)

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您的“上下文”在其任何父项上都没有 Navigator 实例,因为 Navgiator 是在 Scaffold 本身中初始化的。只需将主体包裹起来:在一个新的小部件中,它现在将有一个 parent 导航器。

    Widget showPeriod(int a) {
      return Scaffold(
        appBar: new AppBar(
          title: new Text(
            "Hajar",
            style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
          ),
          backgroundColor: Colors.blue[800],
          actions: <Widget>[
            new Padding(
                padding: EdgeInsets.only(right: 10.0),
                child: Icon(Icons.exit_to_app)),
          ],
        ),
        drawer: new AppDrawer(),
        body: MyPageBody(),
      );
    }
    
    
    class MyPage extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  new Text(
                    "Period #$a",
                    style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.w400),
                  )
                ],
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  new Container(
                    child: Text(
                      "<time>",
                      style: TextStyle(color: Colors.white),
                    ),
                    decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
                      color: Colors.grey,
                    ),
                    padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                  ),
                  SizedBox(width: 10.0),
                  new Container(
                    child: Text(
                      "<Class>",
                      style: TextStyle(color: Colors.white),
                    ),
                    decoration: new BoxDecoration(
                      borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
                      color: Colors.grey,
                    ),
                    padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                  ),
                ],
              ),
              new Padding(
                  padding: EdgeInsets.only(left: 10.0, top: 10.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      new Container(
                        child: Text(
                          "An imaginary subject.",
                          style: TextStyle(color: Colors.white),
                        ),
                        decoration: new BoxDecoration(
                          borderRadius:
                              new BorderRadius.all(new Radius.circular(30.0)),
                          color: Colors.grey,
                        ),
                        padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                      ),
                    ],
                  )),
              SizedBox(
                height: 40.0,
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  new RaisedButton(
                    child: const Text('GO!'),
                    color: Colors.redAccent,
                    elevation: 4.0,
                    splashColor: Colors.red,
                    onPressed: () {
                      // Perform some action
                      Navigator.of(context).pushNamed(TakeAttendance.tag);
                      //Navigator.push(context,takeAttendance());
                    },
                  ),
                  new RaisedButton(
                    child: const Text('Cancel'),
                    color: Colors.blueAccent,
                    elevation: 4.0,
                    splashColor: Colors.blue[200],
                    onPressed: () {
                      // Perform some action
                      //Navigator.pop(context);
                    },
                  ),
                ],
              )
            ]);
      }
    }
    

    【讨论】:

    • 我建议您在 Navigator 上搜索更多内容,有很多很好的用例/最佳实践可用于使用 Flutter 提供的导航系统。我个人喜欢自己构建大部分导航,但如果您根据需要修改常规导航器,它会更强大。
    • @xIsra 我也面临类似的问题。尚未找到解决方案。 *.com/questions/61284614/…