简体   繁体   中英

Flutter error on closing navigator

Navigator operation requested with a context that does not include a Navigator

full code is here

class _MyAppState extends State<MyApp> {
   @override
   Widget build(BuildContext context) {
     return new MaterialApp(
       home: new Scaffold(
         drawer: new Drawer(child: new ListView(
           children: <Widget>[
               new DrawerHeader(

                 decoration:BoxDecoration(

                   color: Colors.blue
                 ),
                 child: new Container(
                   child: new Text("Hearer name"),

                 ),),
               new ListTile(
                 title: new Text("pop1"),

                 onTap: (){Navigator.pop(context);},
               ),
               new ListTile(
                 title: new Text("pop2"),
                 onTap: (){Navigator.pop(context);},
               ),


             new Container(
               padding: const EdgeInsets.all(20.0),
               child: new Text("asdsad"),
               decoration: BoxDecoration(
                 color: Colors.grey
               ),
             ), new ListTile(
                 title: new Text("pop4"),
                 onTap: (){Navigator.pop(context);},
               ),
             new Container(
               padding: const EdgeInsets.all(20.0),
               child: new Text("opt2"),
             ), new Container(
               padding: const EdgeInsets.all(20.0),
               child:
                   new Text("Asdsa"),




             ),




           ],
         ),),
         appBar: new AppBar(
           title: new Text("Hi , i am milla"),

         ),
         body: new Center(
           child: new ListView(
             shrinkWrap: true,
             padding: const EdgeInsets.all(20.0),
             children: <Widget>[
               new ProductItem("orange"),


             ],
           )
         ),
       ),
     );
   }

full error code

: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter ( 3551): The following assertion was thrown while handling a gesture: I/flutter ( 3551): Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): The context used to push or pop routes from the Navigator must be that of a widget that is a I/flutter ( 3551): descendant of a Navigator widget. I/flutter ( 3551): I/flutter ( 3551): When the exception was thrown, this was the stack: I/flutter ( 3551): #0
Navigator.of. (package:flutter/src/widgets/navigator.dart:1179:9) I/flutter ( 3551):

1 Navigator.of (package:flutter/src/widgets/navigator.dart:1186:6) I/flutter ( 3551):

2 Navigator.pop (package:flutter/src/widgets/navigator.dart:1072:22) I/flutter (

3551): #3 _MyAppState.build. (file:///project/flutter_app_2/lib/main.dart:31:38) I/flutter ( 3551):

4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14) I/flutter ( 3551):

5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:549:30) I/flutter ( 3551):

6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24) I/flutter (

3551): #7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9) I/flutter ( 3551): #8
TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7) I/flutter ( 3551): #9
GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) I/flutter ( 3551):

10 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20) I/flutter ( 3551):

11 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22) I/flutter ( 3551):

12 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7) I/flutter ( 3551):

13 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue

(package:flutter/src/gestures/binding.dart:64:7) I/flutter ( 3551):

14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket

(package:flutter/src/gestures/binding.dart:48:7) I/flutter ( 3551):

15 _invoke1 (dart:ui/hooks.dart:134:13) I/flutter ( 3551): #16 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5) I/flutter ( 3551): I/flutter ( 3551): Handler: onTap I/flutter ( 3551):

Recognizer: I/flutter ( 3551):
TapGestureRecognizer#a8d39(debugOwner: GestureDetector, state: ready, won arena, finalPosition: I/flutter ( 3551): Offset(51.9, 239.7), sent tap down) I/flutter ( 3551): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. I/flutter ( 3551): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. D/EGL_emulation( 3551): eglMakeCurrent: 0xa84052a0: ver 2 0 (tinfo 0xa84032d0) Lost connection to device.

I think this code might help you.

in Drawer screen:

drawer: new Drawer(
      child: new Column(
        children: <Widget>[
          new DrawerHeader(
            child: new Container(
              child: new Text("Hearer name"),
            ),
          ),
          new Flexible(
            child: new ListView.builder(
              itemCount: 1,
              itemBuilder: (BuildContext context, int index) {
                return new ListTile(
                  title: new Text("Tap $index"),
                  onTap: () => Navigator.of(context).pop(),
                );
              },
            ),
          )
        ],
      ),
    ),

I've checked your code and it works fine with this dummy code here...

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
 @override
Widget build(BuildContext context) {
 return new MaterialApp(
  title: 'Flutter Demo',
  theme: new ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: new MyHomePage(title: 'Flutter Demo Home Page'),
 );
}
}

 class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
 }

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
    drawer: new Drawer(

      child: new ListView(

      children: <Widget>[
        new DrawerHeader(

          decoration: BoxDecoration(

              color: Colors.blue
          ),
          child:new Text("Hearer name"),

          ),
        new ListTile(
          title: new Text("pop1"),

          onTap: () {
            Navigator.pop(context);
          },
        ),
        new ListTile(
          title: new Text("pop2"),
          onTap: () {
            Navigator.pop(context);
          },
        ),


        new Container(
          padding: const EdgeInsets.all(20.0),
          child: new Text("asdsad"),
          decoration: BoxDecoration(
              color: Colors.grey
          ),
        ), new ListTile(
          title: new Text("pop4"),
          onTap: () {
            Navigator.pop(context);
          },
        ),
        new Container(
          padding: const EdgeInsets.all(20.0),
          child: new Text("opt2"),
        ), new Container(
          padding: const EdgeInsets.all(20.0),
          child:
          new Text("Asdsa"),
        ),
      ],
    ),),
    appBar: new AppBar(
      title: new Text("Hi , i am milla"),
    ),
    body: new Center(
        child: new ListView(
          shrinkWrap: true,
          padding: const EdgeInsets.all(20.0),
          children: <Widget>[
          ],
        )
    ),
  );
  }
 }

You appear to be returning a MaterialApp within your widget appState. This is not going to work. I am assuming you have more than one MaterialApp.
MaterialApp is basically the entry point of your app and should live in main.dart

MaterialApp configures the top-level Navigator.

https://api.flutter.dev/flutter/material/MaterialApp-class.html

each screen should have its own scaffold. The new Keyword is no longer required in Dart

class _MyAppState extends State<MyApp> {
   @override
   Widget build(BuildContext context) {
     return Scaffold(
         drawer: new Drawer(child: new ListView(
           children: <Widget>[
               new DrawerHeader(
                 decoration:BoxDecoration(
                   color: Colors.blue
                 ),
                 child: new Container(
                   child: new Text("Hearer name"),
                 ),),
               new ListTile(
                 title: new Text("pop1"),
                 onTap: (){Navigator.pop(context);},
               ),
               new ListTile(
                 title: new Text("pop2"),
                 onTap: (){Navigator.pop(context);},
               ),
             new Container(
               padding: const EdgeInsets.all(20.0),
               child: new Text("asdsad"),
               decoration: BoxDecoration(
                 color: Colors.grey
               ),
             ), new ListTile(
                 title: new Text("pop4"),
                 onTap: (){Navigator.pop(context);},
               ),
             new Container(
               padding: const EdgeInsets.all(20.0),
               child: new Text("opt2"),
             ), new Container(
               padding: const EdgeInsets.all(20.0),
               child:
                   new Text("Asdsa"),
             ),
           ],
         ),),
         appBar: new AppBar(
           title: new Text("Hi , i am milla"),

         ),
         body: new Center(
           child: new ListView(
             shrinkWrap: true,
             padding: const EdgeInsets.all(20.0),
             children: <Widget>[
               new ProductItem("orange"),

             ],
           )
         ),
       ),
     );
   }

Happy Fluttering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM