【问题标题】:Undefined name 'context'. Try correcting the name to one that is defined, or defining the name未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称
【发布时间】:2020-05-26 18:19:55
【问题描述】:

这是我的代码,但我从不更改页面。错误 = 未定义名称“上下文”

这些是其中的一些:

Undefined name 'context' Undefined name 'context' in flutter navigation

Try correcting the name to one that is defined, or defining the name.

class EntryItem extends StatelessWidget {
const EntryItem(this.entry);

enter code here

final Entry entry;

Widget _buildTiles(Entry root) {
if (root.children.isEmpty)
return ListTile(
title: Text(root.title, style: TextStyle(color: Colors.black)),
trailing: Icon(
Icons.keyboard_arrow_right,
color: Colors.white,
),
onTap: () {
### Navigator.of(context).pushNamedAndRemoveUntil('/gettingStarted', (route) => false)
},
selected: true,
);
return ExpansionTile(
key: PageStorageKey(root),
trailing: Icon(
Icons.keyboard_arrow_right,
),
title: Text(
root.title,
style: TextStyle(color: Color(0xFF2F6CE6)),
),
children: root.children.map(_buildTiles).toList(),
);
}

@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    您必须将 context 从您的构建函数传递给您的 _buildTiles

    Widget _buildTiles(Entry root, BuildContext context) 
    

    还有:

    return _buildTiles(entry, context);
    

    然后使用上下文导航。

    【讨论】:

    • 现在这个错误 = 参数类型 'Widget Function(Entry, BuildContext)' 不能分配给参数类型 'dynamic Function(Entry)'
    • 并且此错误 = 预期 2 个位置参数,但找到了 1 个。尝试添加缺少的参数
    • @Mücahit 我更新了答案。只需将上下文传递给函数。
    • children: root.children.map(_buildTiles).toList(), === > 参数类型'Widget Function(Entry, BuildContext)'不能赋值给参数类型'dynamic函数(条目)'现在这是错误
    【解决方案2】:

    您可以在创建时将上下文从 ListView.builder 传递给 EntryItem 方法,并在 OnClick 中使用它来导航到 OtherPage。 导入'package:flutter/material.dart';

    void main() => runApp(new ExpansionTileSample());
    
    class ExpansionTileSample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            appBar: new AppBar(
              title: const Text('ExpansionTile'),
            ),
            body: new ListView.builder(
              itemBuilder: (BuildContext context, int index) =>
              new EntryItem(data[index], context),
              itemCount: data.length,
            ),
          ),
        );
      }
    }
    
    // One entry in the multilevel list displayed by this app.
    class Entry {
      Entry(this.title, [this.children = const <Entry>[]]);
    
      final String title;
      final List<Entry> children;
    }
    
    // The entire multilevel list displayed by this app.
    final List<Entry> data = <Entry>[
      new Entry(
        'Chapter A',
        <Entry>[
          new Entry(
            'Section A0',
            <Entry>[
              new Entry('Item A0.1'),
              new Entry('Item A0.2'),
              new Entry('Item A0.3'),
            ],
          ),
          new Entry('Section A1'),
          new Entry('Section A2'),
        ],
      ),
      new Entry(
        'Chapter B',
        <Entry>[
          new Entry('Section B0'),
          new Entry('Section B1'),
        ],
      ),
      new Entry(
        'Chapter C',
        <Entry>[
          new Entry('Section C0'),
          new Entry('Section C1'),
          new Entry(
            'Section C2',
            <Entry>[
              new Entry('Item C2.0'),
              new Entry('Item C2.1'),
              new Entry('Item C2.2'),
              new Entry('Item C2.3'),
            ],
          ),
        ],
      ),
    ];
    
    // Displays one Entry. If the entry has children then it's displayed
    // with an ExpansionTile.
    class EntryItem extends StatelessWidget {
      const EntryItem(this.entry, this.context);
    
      final Entry entry;
      final BuildContext context;
    
      Widget _buildTiles(Entry root) {
        if (root.children.isEmpty) return new ListTile(
            onTap: () { print("I can be tapped but I dont't figure out how to navigate");
            Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
              return new OtherScreen();
            }));
            },
            title: new Text(root.title));
        return new ExpansionTile(
          key: new PageStorageKey<Entry>(root),
          title: new Text(root.title),
          children: root.children.map(_buildTiles).toList(),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return _buildTiles(entry);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 2020-09-03
      • 2023-02-07
      • 2020-11-01
      • 2022-08-22
      • 2021-03-24
      • 2022-06-10
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多