我想在http get失败时显示AlertDialog.函数showDialog(https://api.flutter.dev/flutter/material/showDialog.html)有参数"@Required BuildContext context",但是我想从我的异步函数getnews()调用AlertDialog,该函数没有上下文值.
与Java类似,我对没有所有者的对话框使用NULL,我试图将上下文值设置为NULL,但不被接受.
这是我的代码:
Future<dynamic> getNews() async {
dynamic retVal;
try {
var response = await http.get(url));
if (response.statusCode == HttpStatus.ok) {
retVal = jsonDecode(response.body);
}
} catch (e) {
alertDlg(?????????, 'Error', e.toString());
}
return
retVal;
}
static Future<void> alertDlg(context, String titolo, String messaggio) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(titolo),
...
);
}