Flutter 错误:未定义的名称“上下文”

埃莉诺·泰

当我尝试将我的 flutter 页面从 neighborhoodList 推送到个别社区时,我收到了一些错误(例如,neighborhoodlist_admirality)。

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

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

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'),
      ),
    );
  }
}

由于我仍然是初学者,我面临一些错误,并有一些关于这些的问题:

邻居列表.dart 上的错误 1:Navigator.push 下的“未定义名称‘上下文’”--> 不知道为什么会发生这种情况,因为我已经在上面的方法中传递了 BuildContext

邻居列表.dart 上的错误 2:Navigator.push 下的“未定义名称 'neigh1'”--> 我想将 neighborhoodlist.dart 页面重定向到各个街区表,但我不确定我在这里传递的是什么,我试过了'neigh1'(我在 neighbourhoodlist_admirality 中的变量)、'name' - NeighbourhoodList 中的变量和 'index' 0 变量 inNeighbourhoodListState 但到目前为止它们似乎都不起作用。

感谢您对解决此问题的所有帮助,并提前致谢!

恩佐

当你做

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)));
          }
        );
}

您无法访问contextonTap因为此时 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)));
      }
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章