【问题标题】:Flutter error on closing navigator关闭导航器时出现颤振错误
【发布时间】:2019-02-01 17:31:46
【问题描述】:

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

完整代码在这里

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


             ],
           )
         ),
       ),
     );
   }

完整的错误代码

: ══╡ 手势捕捉到的异常 ╞═════════════════════════════════════════════════ ══════════════════ I/flutter (3551):在处理 手势:I/flutter(3551):导航器操作请求 不包括导航器的上下文。我/颤动(3551): 用于从导航器推送或弹出路由的上下文必须是 一个 I/flutter 小部件(3551):导航器的后代 小部件。 I/flutter (3551): I/flutter (3551): 当异常发生时 抛出,这是堆栈: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/颤振(3551):处理程序:onTap I/颤振(3551):

识别器:I/flutter (3551):
TapGestureRecognizer#a8d39(debugOwner:GestureDetector,状态:就绪, 赢得竞技场,finalPosition:I/flutter (3551):Offset(51.9, 239.7), 发送轻击)I/flutter(3551): ══════════════════════════════════════════════════ ══════════════════════════════════════════════════ I/flutter (3551): 另一个异常被抛出:导航器操作 使用不包含导航器的上下文请求。我/颤动 (3551):引发另一个异常:请求导航器操作 具有不包括导航器的上下文。我/颤振(3551): 引发了另一个异常:请求导航器操作 不包括导航器的上下文。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。我/颤动(3551):另一个 抛出异常:使用上下文请求导航器操作 不包括导航器。 D/EGL_emulation(3551): eglMakeCurrent: 0xa84052a0: ver 2 0 (tinfo 0xa84032d0) 失去连接 到设备。

【问题讨论】:

  • 可能是您错误地重复了 MaterialApp 小部件。能否请您添加顶部代码(Stateless Widgets)以清除错误。

标签: flutter flutter-layout


【解决方案1】:

我检查了你的代码,这里的虚拟代码可以正常工作...

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

【讨论】:

  • 的方法有效(将家庭内容包装到另一个小部件中).. 不知道它是如何工作的 :-(
【解决方案2】:

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

在抽屉屏幕中:

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

【讨论】:

    【解决方案3】:

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

    MaterialApp 配置顶级 Navigator。

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

    快乐飘扬

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-26
      • 2020-04-01
      • 2021-06-17
      • 1970-01-01
      • 2020-07-14
      • 2022-06-16
      • 2020-06-20
      • 2018-10-11
      相关资源
      最近更新 更多