Flutter使用Navigator跳转时报Undefined name 'context'错误如何解决
问题原因
context是StatelessWidget/StatefulWidget的build方法的入参,仅能在build方法内部的作用域中访问。当前list1/list2/list3被定义为类的全局属性,不在build的作用域范围内,所以无法读取到context变量,触发未定义报错。- 额外语法问题:部分
Navigator.push代码结尾多写了多余的闭合括号,也会触发编译报错。
修复方案
第一步:调整列表的定义位置
把list1/list2/list3三个列表的定义,从类的属性位置挪到build方法内部,放在return语句之前即可,这样就能正常访问到build方法传入的context变量。
第二步:修正多余的语法符号
删除所有Navigator.push代码段里多余的闭合括号。
(可选)代码规范优化
Flutter官方要求类名使用大驼峰命名,建议把chooseCategory类名改为ChooseCategory,避免后续潜在的规范类报错。
修正后的核心代码示例
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) { // 三个列表挪到build方法内部定义 List<Widget> list1 = <Widget>[ ListTile( title: Text('Shoes', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/men1.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Clothing', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), subtitle: Text('429 Castro St'), leading: Image.asset("assets/Images/men2.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Accessories', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/men3.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ]; List<Widget> list2 = <Widget>[ ListTile( title: Text('Shoes', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), subtitle: Text('85 W Portal Ave'), leading: Image.asset("assets/Images/women1.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Clothing', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/women2.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Accessories', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/women3.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ]; List<Widget> list3 = <Widget>[ ListTile( title: Text('Boys Shoes', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/boy1.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Girls Shoes', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/girl1.png"), trailing: Icon(Icons.arrow_forward), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Boys Clothing', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/boy1.png"), trailing: Icon(Icons.arrow_forward), onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, ), ListTile( title: Text('Girls Clothing', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)), leading: Image.asset("assets/Images/girl2.png"), onTap: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Productpage())); }, trailing: Icon(Icons.arrow_forward), ), ]; return MaterialApp( debugShowCheckedModeBanner: false, home: DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( title: const Text('Collections'), backgroundColor: Colors.grey[400], actions: [ PopupMenuButton( icon: Icon(Icons.menu), color: Colors.blue, itemBuilder: (context) => [ PopupMenuItem<int>( value: 0, child: Text( "Home", style: TextStyle(color: Colors.white),), ), PopupMenuItem<int>( value: 1, child: Text( "About", style: TextStyle(color: Colors.white),), ), PopupMenuItem<int>( value: 2, child: Text( "Settings", style: TextStyle(color: Colors.white),), ), ], onSelected: (item) => {print(item)}, ), ], bottom: TabBar(isScrollable: true, tabs: [ Tab(text: 'MEN',), Tab(text: 'WOMEN',), Tab(text: 'KIDS',), ]), ), body: TabBarView( children: [ ListView( children: list1, ), ListView( children: list2, ), ListView( children: list3, ), ], ), bottomNavigationBar: BottomNavigationBar( type: BottomNavigationBarType.fixed, items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: Text('Home').data, ), BottomNavigationBarItem( icon: Icon(Icons.business), label: Text('Business').data, ), BottomNavigationBarItem( icon: Icon(Icons.computer), label: Text('Technology').data, ), BottomNavigationBarItem( icon: Icon(Icons.book), label: Text('Education').data, ), ], ), ), )); } }
内容的提问来源于stack exchange,提问作者Rashmi Gunawardana
相关产品推荐
相关产品推荐

