Запрошена операция навигатора с контекстом, который не включает в себя навигатор - флаттер - PullRequest
1 голос
/ 22 марта 2020

У меня проблема с навигатором в моем файле флаттера.

Проблема в моем 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}')
    );
  }
}

1 Ответ

1 голос
/ 22 марта 2020

Попробуйте изменить ваши методы args как.

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

И передать контекст, в котором вы его вызвали.

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