繁体   English   中英

关闭导航器时出现颤振错误

[英]Flutter error on closing navigator

使用不包含导航器的上下文请求的导航器操作

完整的代码在这里

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"),


             ],
           )
         ),
       ),
     );
   }

完整的错误代码

:══╡手势引起的异常CA ══════════════════════════I / flutter(3551):处理手势时引发了以下断言:I / flutter(3551):请求的导航器操作使用了不包含导航器的上下文。 I / flutter(3551):用于从导航器推送或弹出路由的上下文必须是作为I / flutter(3551):Navigator小部件的后代的窗口小部件的上下文。 I / flutter(3551):I / flutter(3551):引发异常时,这是堆栈:I / flutter(3551):#0
Navigator.of。 (包: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(包:flutter / src / widgets / navigator.dart:1072:22)I / flutter(

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

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

5 _InkResponseState.build。 (包装: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(包:flutter / src / gestures / tap.dart:123:7)I / flutter(3551):#9
GestureArenaManager.sweep(包: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

(包: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 ):处理程序:onTap I / flutter(3551):

识别器:I /颤振(3551):
TapGestureRecognizer#a8d39(debugOwner:GestureDetector,state:ready,won arena,finalPosition:I / flutter(3551):Offset(51.9,239.7),tap tap down)I / flutter(3551):════════ ══════════════════════════════════════════════════ ══════════════════════════════════════════I / flutter(3551):另一个引发异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 I / flutter(3551):引发了另一个异常:请求的导航器操作具有不包含导航器的上下文。 D / EGL_emulation(3551):eglMakeCurrent:0xa84052a0:ver 2 0(tinfo 0xa84032d0)断开与设备的连接。

我认为这段代码可能会对您有所帮助。

在抽屉屏幕中:

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

我已经检查了您的代码,并且在这里使用此虚拟代码可以正常工作...

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

您似乎在小部件appState中返回了MaterialApp。 这是行不通的。 我假设您有多个MaterialApp。
MaterialApp基本上是您应用程序的切入点,应该位于main.dart中

MaterialApp配置顶级导航器。

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

每个屏幕都应该有自己的支架。 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"),

             ],
           )
         ),
       ),
     );
   }

快乐飘飘

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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