如何解决参数类型 'Future<bool>' 不能分配给参数类型 'Widget' [Flutter]
Future _alertDialog(BuildContext context,{String isi,AlertType alert}) {
return Alert(
context: context,type: alert,title: "Notification",desc: "Laporan telah masuk,silahkan menunggu email dari admin!",buttons: [
DialogButton(
child: Text(
"OKAY",style: TextStyle(color: Colors.white,fontSize: 20),),onpressed: () => Navigator.pop(
context,width: 120,)
],).show();
}
您好,我还是 Flutter 新手,我想尝试在另一个类中创建警报对话框,因此我将其设为可移植类以轻松使用警报。
例如,
我创建了 alertDialog.dart
,我想在 alertDialog.dart
内调用 main.dart
。
但是我出错了
参数类型“Future”不能分配给参数类型“Widget”
对于警报,我使用了另一个依赖
rFlutter_alert: ^2.0.2
我正在尝试这个是因为我不了解上下文,我总是得到
查找已停用小部件的祖先是不安全的
有什么解决办法吗? 先谢谢你!
解决方法
函数 _alertDialog
返回一个 Future
,那么您期望什么?您不能在小部件树中使用 _alertDialog
,因为它不是小部件而是一个函数。相反,将 _alertDialog
称为按钮或类似内容的 onPressed
属性。
我假设 Alert(/*...*/).show();
会返回一个 future,因为它正在等待用户响应,例如单击按钮。当 () => Navigator.pop(context)
在您的 onPressed
的 DialogButton
方法中被调用时,未来可能会完成。
显示提示功能可以作废
void _showAlert(BuildContext context) {
showDialog(
context: context,builder: (context) => AlertDialog(
title: Text("Wifi"),content: Text("Wifi not detected. Please activate it."),)
);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。