我正在一个单独的文件中制作一个show对话框函数,然后将其调用到另一个文件中,向函数提供4个参数,包括BuildContext参数,如图所示
void showDialogSingleButton(BuildContext context, String title, String
message, String buttonLabel) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title:
new Text(title),
content:
new Text(message),
actions: <Widget>[
new FlatButton(
child:
new Text(buttonLabel),
onPressed: () {
Navigator.
of(context).pop();
},
),
],
);
},
);
}
并在此文件中将其称为将来的对象,作为显示对话框功能:
Future launchURL(String url) async {
if(await canLaunch(url)) {
await launch(url, forceSafariVC: true, forceWebView: true);
}
else {
showDialogSingleButton( context, "Unable to reach your website.",
"Currently unable to reach the website $URL. Please try again at a later
time.", "OK");
}
}
当我调用该方法时,由于某种原因,它给了我这个错误:
Compiler message:
lib/screens/login/login_page.dart:45:31: Error: Getter not found:
'context'.
showDialogSingleButton( context, "Unable to reach your website.",
"Currently unable to reach the website $URL. Please try again at a
later
time.", "OK");
^^^^^^^
lib/screens/login/login_page.dart:45:31: Error: The getter 'context'
isn't defined for the class 'LoginScreen'.
- 'LoginScreen' is from
'package:gam3ity_aa/screens/login/login_page.dart'
('lib/screens/login/login_page.dart').
Try correcting the name to the name of an existing getter, or defining
a getter or field named 'context'.
showDialogSingleButton( context, "Unable to reach your website.",
"Currently unable to reach the website $URL. Please try again at a
later
time.", "OK");
答案 0 :(得分:0)
在您的launchUrl
函数中,context
可能不存在,如果不看更多代码就很难说出来。
一种继续调试的好方法是将BuildContext作为参数添加到函数launchUrl
中。无论如何,您都应该这样做,launchUrl
不应只是更好地,更易于维护的代码来引用类级别的变量,以使函数需要引用的每个变量都明确。
Future launchURL(String url) async {
if(await canLaunch(url)) {
await launch(url, forceSafariVC: true, forceWebView: true);
}
else {
//HERE, DOES CONTEXT EXIST??
showDialogSingleButton( context, "Unable to reach your website.",
"Currently unable to reach the website $URL. Please try again at a later
time.", "OK");
}
}