【问题标题】:The argument type 'Future<bool>' can't be assigned to the parameter type 'Widget' [Flutter]参数类型“Future<bool>”不能分配给参数类型“Widget”[Flutter]
【发布时间】:2021-07-06 05:49:13
【问题描述】:
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,我想在main.dart 中调用alertDialog.dart。 但我得到了错误

参数类型“Future”不能分配给参数类型“Widget”

对于警报,我使用了另一个依赖项

rflutter_alert: ^2.0.2

我正在尝试这个,因为我不了解上下文,我总是得到

查找已停用小部件的祖先是不安全的

有什么解决办法吗? 之前谢谢你!

【问题讨论】:

    标签: firebase flutter widget future


    【解决方案1】:

    函数_alertDialog 返回一个Future,那么你期待什么呢?您不能在小部件树中使用_alertDialog,因为它不是小部件而是函数。而是将_alertDialog 调用为按钮的onPressed 属性或类似属性。

    我假设,Alert(/*...*/).show(); 返回一个未来,因为它正在等待用户响应,例如单击按钮。当() =&gt; Navigator.pop(context) 在您的DialogButtononPressed 方法中被调用时,未来可能会完成。

    【讨论】:

    • 好吧,如果小部件返回 Future,我确实了解它,但我认为它会更便携,然后只需在我拥有的每个文件中制作 alertDialog,就像我制作的 main.dart新警报,在另一个屏幕上我发出新警报。所以我想如果我能把它变成一个新文件那么容易调用它会很有帮助。
    • 你说得对,在单独的文件中移动小部件和相关方法也是更好的风格。老实说,我只是不明白为什么它仍然不起作用。如果在按钮的 onPressed 处理程序中调用 _alertDialog(/*...*/) 是否还会出现错误?
    【解决方案2】:

    显示提醒功能可以作废

    void _showAlert(BuildContext context) {
          showDialog(
              context: context,
              builder: (context) => AlertDialog(
                title: Text("Wifi"),
                content: Text("Wifi not detected. Please activate it."),
              )
          );
        }
    

    【讨论】:

    • 嗯,是的,我确实使用 Future 或者只是使用 _showAlert(BuildContext context) 之类的名称而不使用任何方法名称,但感谢您的回答!
    猜你喜欢
    • 2021-11-24
    • 2021-10-16
    • 2021-10-10
    • 2020-08-23
    • 2021-04-07
    • 2019-12-06
    • 2021-06-04
    • 2021-01-05
    • 2021-12-30
    相关资源
    最近更新 更多