错误:颤动中未定义的名称“上下文”

Posted

技术标签:

【中文标题】错误:颤动中未定义的名称“上下文”【英文标题】:error: Undefined name 'context' in flutter 【发布时间】:2021-10-12 03:24:11 【问题描述】:

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

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

这些是其中的一些:

未定义名称“上下文”

颤振导航中未定义的名称“上下文”

import 'package:flutter/material.dart';
import 'package:origitive_app/main.dart';
import 'package:origitive_app/Productpage.dart';

void main() 
  runApp(chooseCategory());


class chooseCategory extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return new MaterialApp(
        debugShowCheckedModeBanner: false,
        home: new DefaultTabController(
          length: 3,
          child: new Scaffold(
            appBar: new AppBar(
              title: const Text('Collections'),
              backgroundColor: Colors.grey[400],

              actions: [
                //list if widget in appbar actions
                PopupMenuButton(
                  icon: Icon(Icons.menu),
                  //don't specify icon if you want 3 dot menu
                  color: Colors.blue,
                  itemBuilder: (context) =>
                  [
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Home", style: TextStyle(color: Colors.white),),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "About", style: TextStyle(color: Colors.white),),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Settings", style: TextStyle(color: Colors.white),),
                    ),
                  ],
                  onSelected: (item) => print(item),
                ),
              ],
              bottom: new TabBar(isScrollable: true, tabs: [
                new Tab(text: 'MEN',),
                new Tab(text: 'WOMEN',),
                new Tab(text: 'KIDS',),
              ]),
            ),
            body: new TabBarView(
              children: [
                new ListView(
                  children: list1,
                ),

                new ListView(
                  children: list2,
                ),
                new ListView(
                  children: list3,
                ),
              ],
            ),

            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text('Home'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.business),
                  title: Text('Business'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.computer),
                  title: Text('Technology'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.book),
                  title: Text('Education'),
                ),
              ],


            ),

          ),
        ));
  

  List<Widget> list1 = <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,

    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('429 Castro St'),
      leading: new Image.asset("assets/Images/men2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,


    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];

  List<Widget> list2 = <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('85 W Portal Ave'),
      leading: new Image.asset("assets/Images/women1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];
  List<Widget> list3 = <Widget>[
    new ListTile(
      title: new Text('Boys Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/boy1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage());
        );
      ,
    ),
    new ListTile(
      title: new Text('Girls Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Boys Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),

         leading: new Image.asset("assets/Images/boy1.png"),
         trailing: Icon(Icons.arrow_forward),
          onTap: () 
            Navigator.push(
                context,//error
                MaterialPageRoute(builder: (context) => Productpage()));
            );
          ,
    ),
    new ListTile(
      title: new Text('Girls Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl2.png"),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,trailing: Icon(Icons.arrow_forward),
    ),
  ];


尝试添加静态构建上下文但失败。它给出了一个空错误。

【问题讨论】:

【参考方案1】:

这是因为您试图访问其范围之外的上下文。像这样将 list1 和 list2 移动到方法 build 中:-

import 'package:flutter/material.dart';
import 'package:origitive_app/main.dart';
import 'package:origitive_app/Productpage.dart';

void main() 
  runApp(chooseCategory());


class chooseCategory extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return new MaterialApp(
        debugShowCheckedModeBanner: false,
        home: new DefaultTabController(
          length: 3,
          child: new Scaffold(
            appBar: new AppBar(
              title: const Text('Collections'),
              backgroundColor: Colors.grey[400],

              actions: [
                //list if widget in appbar actions
                PopupMenuButton(
                  icon: Icon(Icons.menu),
                  //don't specify icon if you want 3 dot menu
                  color: Colors.blue,
                  itemBuilder: (context) =>
                  [
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Home", style: TextStyle(color: Colors.white),),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "About", style: TextStyle(color: Colors.white),),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Settings", style: TextStyle(color: Colors.white),),
                    ),
                  ],
                  onSelected: (item) => print(item),
                ),
              ],
              bottom: new TabBar(isScrollable: true, tabs: [
                new Tab(text: 'MEN',),
                new Tab(text: 'WOMEN',),
                new Tab(text: 'KIDS',),
              ]),
            ),
            body: new TabBarView(
              children: [
                new ListView(
                  children: list1,
                ),

                new ListView(
                  children: list2,
                ),
                new ListView(
                  children: list3,
                ),
              ],
            ),

            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text('Home'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.business),
                  title: Text('Business'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.computer),
                  title: Text('Technology'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.book),
                  title: Text('Education'),
                ),
              ],


            ),

          ),
        ));

        List<Widget> list1 = <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,

    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('429 Castro St'),
      leading: new Image.asset("assets/Images/men2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,


    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];

  List<Widget> list2 = <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('85 W Portal Ave'),
      leading: new Image.asset("assets/Images/women1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];
  List<Widget> list3 = <Widget>[
    new ListTile(
      title: new Text('Boys Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/boy1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage());
        );
      ,
    ),
    new ListTile(
      title: new Text('Girls Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Boys Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),

         leading: new Image.asset("assets/Images/boy1.png"),
         trailing: Icon(Icons.arrow_forward),
          onTap: () 
            Navigator.push(
                context,//error
                MaterialPageRoute(builder: (context) => Productpage()));
            );
          ,
    ),
    new ListTile(
      title: new Text('Girls Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl2.png"),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,trailing: Icon(Icons.arrow_forward),
    ),
  ];
  

【讨论】:

我试过这样做,但它排除了整个三个列表,但我找不到原因。没有评论者被应用到他们身上。【参考方案2】:

这是修改后的代码

import 'package:flutter/material.dart';
import 'package:origitive_app/main.dart';
import 'package:origitive_app/Productpage.dart';

void main() 
  runApp(chooseCategory());


class chooseCategory extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return new MaterialApp(
        debugShowCheckedModeBanner: false,
        home: new DefaultTabController(
          length: 3,
          child: new Scaffold(
            appBar: new AppBar(
              title: const Text('Collections'),
              backgroundColor: Colors.grey[400],
              actions: [
                //list if widget in appbar actions
                PopupMenuButton(
                  icon: Icon(Icons.menu),
                  //don't specify icon if you want 3 dot menu
                  color: Colors.blue,
                  itemBuilder: (context) => [
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Home",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "About",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    PopupMenuItem<int>(
                      value: 0,
                      child: Text(
                        "Settings",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ],
                  onSelected: (item) => print(item),
                ),
              ],
              bottom: new TabBar(isScrollable: true, tabs: [
                new Tab(
                  text: 'MEN',
                ),
                new Tab(
                  text: 'WOMEN',
                ),
                new Tab(
                  text: 'KIDS',
                ),
              ]),
            ),
            body: new TabBarView(
              children: [
                new ListView(
                  children: list1(context),
                ),
                new ListView(
                  children: list2(context),
                ),
                new ListView(
                  children: list3(context),
                ),
              ],
            ),
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text('Home'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.business),
                  title: Text('Business'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.computer),
                  title: Text('Technology'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.book),
                  title: Text('Education'),
                ),
              ],
            ),
          ),
        ));
  

  List<Widget> list1(BuildContext context) => <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,

    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('429 Castro St'),
      leading: new Image.asset("assets/Images/men2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,


    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/men3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];

  List<Widget> list2(BuildContext context) => <Widget>[
    new ListTile(
      title: new Text('Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      subtitle: new Text('85 W Portal Ave'),
      leading: new Image.asset("assets/Images/women1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women2.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Accessories',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/women3.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
  ];

  List<Widget> list3(BuildContext context) => <Widget>[
    new ListTile(
      title: new Text('Boys Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/boy1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage());
        );
      ,
    ),
    new ListTile(
      title: new Text('Girls Shoes',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl1.png"),
      trailing: Icon(Icons.arrow_forward),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,
    ),
    new ListTile(
      title: new Text('Boys Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),

         leading: new Image.asset("assets/Images/boy1.png"),
         trailing: Icon(Icons.arrow_forward),
          onTap: () 
            Navigator.push(
                context,//error
                MaterialPageRoute(builder: (context) => Productpage()));
            );
          ,
    ),
    new ListTile(
      title: new Text('Girls Clothing',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Image.asset("assets/Images/girl2.png"),
      onTap: ()
        Navigator.push(
            context,//error
            MaterialPageRoute(builder: (context) => Productpage()));
      ,trailing: Icon(Icons.arrow_forward),
    ),
  ];

【讨论】:

【参考方案3】:

在我的情况下,我必须将小部件转换为有状态小部件才能在其状态函数中使用上下文(即在 build() 之外)

【讨论】:

【参考方案4】:

我必须将我的小部件转换为有状态的小部件并且它可以工作

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于错误:颤动中未定义的名称“上下文”的主要内容,如果未能解决你的问题,请参考以下文章

Nextjs:getStaticProps 中未定义的上下文

Struct 上下文中未使用 printf 时的段错误

颤振未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称

未定义的名称“上下文”(在 Flutter 导航中)

listView 颤动中未定义子项和填充

如何传递上下文以在颤动中显示 AlertDialog