在 Future<Widget> 函数 Flutter 中调用小部件

问题描述

基本上我有一个按钮(GestureDetector)可以在同一个文件调用 Future 函数。问题是该函数中的小部件未按预期显示,但后台进程已成功运行。

触发器:

showDialog(
context: context,builder: (context) {
AlertDialog(
  /// Below the button to call function *resetPassword*
  GestureDetector(
   child: Text("Yes"),onTap: () async {
   Navigator.of(context).pop();
   resetPassword('manan@gmail.com')}))})

小部件功能

Future<Widget> resetPassword(email) async {
try{
await FirebaseAuth.instance.sendPasswordResetEmail(email: email)
return AlertDialog(
///the content of dialog)
}on FirebaseAuthException catch (e) {
return AlertDialog(
///the content of dialog)
}}

出人意料的是,重置密码的邮件发送成功了。
免责声明:我是 Flutter 新手,希望 sifus 能考虑一下。

解决方法

一般来说,当您使用对话框时,您必须使用 showDialog() 方法对其进行包装,例如:

  await showDialog(
   context: context,builder: (BuildContext context) {
    return AlertDialog(
      title: Center(child: Text('Reset password')),content: Builder(
        builder: (context) {
          return Container(child: ForgotPasswordForm());
        },),);
  },);

其次,我看到您嵌套了警报对话框小部件,我认为您应该对其进行重组。