颤振错误:未定义的名称"context"



当我尝试将我的颤振页面从邻域列表推送到单个邻域(例如neighborhoodlist_admirality)时收到一些错误。

在我的邻居列表中,当用户点击相关邻居时,我希望导航到各个邻居页面。由于我还没有建立单独的社区页面,我已经将它们链接到一个示例页面,即neighboradadmirality。

这是我的邻居列表页面的代码:

import 'package:flutter/material.dart';
import 'indv_neighbourhoods/neighbourhoodlist_admirality.dart';
class NeighbourhoodList extends StatefulWidget {
NeighbourhoodList({ this.name = "name"});
final String name;
@override
_NeighbourhoodListState createState() => _NeighbourhoodListState();
}
class _NeighbourhoodListState extends State<NeighbourhoodList> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Neighbourhoods',
),
),
body: _buildListView(context),
);
}
ListView _buildListView(BuildContext context){
return ListView.builder(
itemCount: allNeighbourhoods.length,
itemBuilder: (BuildContext content, int index) {
NeighbourhoodList neighbourhoodlist = allNeighbourhoods[index];
return NeighbourhoodListTile(neighbourhoodlist);
});
}
}
class NeighbourhoodListTile extends ListTile {
NeighbourhoodListTile(NeighbourhoodList neighbourhoodlist)
: super(
title: Text(neighbourhoodlist.name),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(
context, 
MaterialPageRoute(builder: (context) => NeighbourhoodAdmiralty(neigh1)),
);
}
);
}
List<NeighbourhoodList> allNeighbourhoods = [
NeighbourhoodList(name: 'Admiralty'),
NeighbourhoodList(name: 'Aljunied'),
NeighbourhoodList(name: 'Ang Mo Kio'),
];

这是一个示例页面的代码,我想在每个单独的邻居被定向时将我的邻居列表定向到。

import 'package:flutter/material.dart';
class NeighbourhoodAdmiralty extends StatefulWidget {
final String neigh1;
NeighbourhoodAdmiralty(this.neigh1);
@override
_NeighbourhoodAdmiraltyState createState() => _NeighbourhoodAdmiraltyState();
}
class _NeighbourhoodAdmiraltyState extends State<NeighbourhoodAdmiralty> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Admiralty"),
),
body: Center(child: Text('This is the individual neighbourhood page'),
),
);
}
}

由于我还是一个初学者,我面临着一些错误,并有一些问题:

邻域列表错误1。dart: "未定义名称'context'"在导航器。推动——比;不知道为什么会发生这种情况,因为我已经在上面的方法中传递了BuildContext

邻域列表错误2。dart: "未定义名称' neighbor '"在导航器。推动——比;我想重新安排一下邻居名单。dart页面到个别邻居表,但我不确定我在这里传递什么,我已经尝试过' neighbor1 '(我的变量在neighborhoodlist_admirality), 'name' -在neighborhoodlist变量,和'index' 0在neighborhoodliststate变量,但它们似乎都没有工作到目前为止。

感谢您在解决此问题方面的所有帮助,并提前表示感谢!

当你这么做的时候

class NeighbourhoodListTile extends ListTile {
NeighbourhoodListTile(NeighbourhoodList neighbourhoodlist)
: super(
title: Text(neighbourhoodlist.name),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => NeighbourhoodAdmiralty(neigh1)));
}
);
}

你不能访问onTap中的context,因为Flutter在这一点上还没有提供给你。如果您需要context,请使用StatelessWidget,您可以在build方法中访问它:

class NeighbourhoodListTile extends StatelessWidget {
final NeighbourhoodList neighbourhoodlist;
const NeighbourhoodListTile(this.neighbourhoodlist);
@override
Widget build(BuildContext context) {
//  Here's your context ^^^^^^^
return ListTile(
title: Text(neighbourhoodlist.name),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => NeighbourhoodAdmiralty(neigh1)));
}
);
}
}

对于第二个错误,此时也没有neigh1变量。我不知道你的逻辑是什么,但我认为你想用neighbourhoodlist.name:

代替它。
class NeighbourhoodListTile extends StatelessWidget {
final NeighbourhoodList neighbourhoodlist;
const NeighbourhoodListTile(this.neighbourhoodlist);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(neighbourhoodlist.name),
trailing: Icon(Icons.arrow_forward),
onTap: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => NeighbourhoodAdmiralty(neighbourhoodlist.name)));
}
);
}
}

最新更新