使用不包含导航器的上下文请求的导航器操作
完整代码在这里
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"),
],
)
),
),
);
}
完整的错误代码
:BY手势引起的异常 ╞═════════════════════════════════════════════════ ══════════════════ I / flutter(3551):处理以下内容时引发了以下断言 手势:I / flutter(3551):请求导航器操作,并带有 不包含导航器的上下文。 I / flutter(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(包: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(程序包: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 (包装: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 /颤振器(3551):
TapGestureRecognizer#a8d39(debugOwner:GestureDetector,状态:就绪, 赢了竞技场,finalPosition:I / flutter(3551):Offset(51.9,239.7), 发送拍打下来)I / flutter(3551): ══════════════════════════════════════════════════ ══════════════════════════════════════════════════ I / flutter(3551):引发了另一个异常:导航器操作 请求的上下文不包含导航器。我/扑 (3551):引发了另一个异常:请求了Navigator操作 具有不包含导航器的上下文。 I /颤振(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)丢失的连接 到设备。
答案 0 :(得分: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(),
);
},
),
)
],
),
),
答案 1 :(得分:0)
我已经检查了您的代码,并且在这里使用此虚拟代码可以正常工作...
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 :(得分:0)
您似乎在小部件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"),
],
)
),
),
);
}
快乐飘飘