【问题标题】:Navigator operation requested with a context that does not include a Navigator - Flutter使用不包含 Navigator 的上下文请求的 Navigator 操作 - Flutter
【发布时间】:2020-03-22 11:44:41
【问题描述】:

我的颤振文件中的导航器有问题。

问题出在我的GestureDetector _listItem 中,点击对象后,在我的调试控制台中抛出我:

The following assertion was thrown while handling a gesture:
Navigator operation requested with a context that does not include a Navigator.

The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

我不知道为什么这对我不起作用,有人可以帮助我吗?

下面是我的代码:

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(100.0),
        child: AppBar(
          iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
            backgroundColor: Colors.white,
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.shopping_cart,
                  color: Color.fromRGBO(9, 133, 46, 100),
                ),
                onPressed: (){
                  print('klikniete');
                },
              ),
            ],
        ),
      ),
      body: Builder(
          builder: (context) => Container(
            child: FutureBuilder(
              future: fetchOrders(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (_ordersForDisplay.length == null) {
                  return Container(
                    child: Center(child: Text("Ładowanie...")),
                  );
                } else {
                  return ListView.builder(
                    itemCount: _ordersForDisplay.length + 1,
                    itemBuilder: (BuildContext context, int index) {
                      return index == 0 ? _searchBar() : _listItem(index - 1);
                    },
                  );
                }
              },
            ),
          ),
        ), 
      )
    );
  }



  _listItem(index) {
    return GestureDetector(
      onTap: () => Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => DetailPage(item: _ordersForDisplay[index])),
      ),
      child: Card(
        child: Padding(
          padding: const EdgeInsets.only(
              top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                _ordersForDisplay[index].firstName,
                style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
              ),
              Text(
                _ordersForDisplay[index].lastName,
                style: TextStyle(
                  color: Colors.grey.shade600
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  final Order item;

  const DetailPage({this.item});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('${item.firstName} - ${item.lastName}')
    );
  }
}

【问题讨论】:

标签: flutter navigator


【解决方案1】:

尝试改变你的方法 args as。

_listItem(index, context) {  //Changes here
 ....
 }

并传递你调用它的上下文。

 return index == 0 ? _searchBar() : _listItem(index - 1,context);

【讨论】:

  • 你能告诉我如何将我的搜索栏放入应用栏吗?我仍然尝试这样做但没有成功:(
  • 现在,他在 appbar 下,但我希望他能在 appbar 中找到,最好在最底部
猜你喜欢
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2021-02-19
  • 2021-05-23
  • 2018-09-17
  • 2021-08-02
相关资源
最近更新 更多