【问题标题】:Flutter Errors: Undefined name "context"颤振错误:未定义的名称“上下文”
【发布时间】:2021-07-24 23:58:26
【问题描述】:

当我尝试将我的颤振页面从 neighbourhoodList 推送到各个社区(例如 neighbourhoodlist_admirality)时收到一些错误。

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

这是我的 neighbourhoodlist 页面代码:

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

由于我还是一个初学者,我遇到了一些错误,并且有几个问题:

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

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

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

【问题讨论】:

    标签: flutter


    【解决方案1】:

    当你这样做时

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

    【讨论】:

    • 感谢 Enzo,这解决了我的第一个错误!请问您是否知道如何解决 neigh1 下面的红色波浪线?
    • @EleanorTay 我已经更新了我的答案,看看这是否能解决你的问题!
    • 嗨@enzo,这正是我需要的,非常感谢!
    猜你喜欢
    • 2021-11-06
    • 2021-03-28
    • 1970-01-01
    • 2021-10-29
    • 2021-10-12
    • 2021-05-01
    • 2020-11-01
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多