在 Flutter 中的 MaterialPageRoute 之后尝试显示对话时出现“'context != null': is not true”错误?
Getting "'context != null': is not true" error when trying to showDialogue after a MaterialPageRoute in Flutter?
我目前能够从 Flutter 的 Home App 将 MaterialRoute 路由到一个页面,并显示一个弹出对话框。但是,从第二页路由到第三页(其中包含一个应该显示对话的按钮)时,我收到此错误:[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/localizations.dart': Failed assertion: line 446 pos 12: 'context != null': is not true.
触发该错误的 showDialogue 如下所示:
class ThirdPageWidgetState extends State<ThirdPageWidget> {
StreamSubscription<ScanResult> scanSubscription;
@override
void initState() {
super.initState();
}
Future<void> alert(deviceName) async {
return showDialog<void>(
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Button Pressed!'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('test'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
),
],
);
},
);
}
'Build function omitted'
}
而第二页到第三页的路由是这样的:
void routeAppToThirdPage() async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ThirdPageWidget(),
),
);
}
您收到此错误的原因是您没有将 BuildContext
传递给 showDialog
,您必须进行更改。
Future<void> alert(deviceName, context) async {...}
//Then when you call your function in your build function you would pass in context
await alert(deviceName, context);
showDialogue<void>()
需要一个 context:context
参数,它没有被编译器捕获。
return showDialog<void>(
context: context // THIS WAS MISSING
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Button Pressed!'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('test'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
),
],
);
```
我目前能够从 Flutter 的 Home App 将 MaterialRoute 路由到一个页面,并显示一个弹出对话框。但是,从第二页路由到第三页(其中包含一个应该显示对话的按钮)时,我收到此错误:[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/localizations.dart': Failed assertion: line 446 pos 12: 'context != null': is not true.
触发该错误的 showDialogue 如下所示:
class ThirdPageWidgetState extends State<ThirdPageWidget> {
StreamSubscription<ScanResult> scanSubscription;
@override
void initState() {
super.initState();
}
Future<void> alert(deviceName) async {
return showDialog<void>(
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Button Pressed!'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('test'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
),
],
);
},
);
}
'Build function omitted'
}
而第二页到第三页的路由是这样的:
void routeAppToThirdPage() async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ThirdPageWidget(),
),
);
}
您收到此错误的原因是您没有将 BuildContext
传递给 showDialog
,您必须进行更改。
Future<void> alert(deviceName, context) async {...}
//Then when you call your function in your build function you would pass in context
await alert(deviceName, context);
showDialogue<void>()
需要一个 context:context
参数,它没有被编译器捕获。
return showDialog<void>(
context: context // THIS WAS MISSING
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Button Pressed!'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('test'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
),
],
);
```