[英]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.