【问题标题】:I want to add an Alert Dialog in my Catch block in flutter我想在我的 Catch 块中添加一个警报对话框
【发布时间】:2019-10-15 04:43:19
【问题描述】:

此代码用于 FireBase 注册

onPressed: () async {
    if (_emailController.text.isEmpty || _passwordController.text.isEmpty) {
        throw _showDialog2(context);
    }
    FirebaseUser user;
    if (user == null) {
        try {
            user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
            _showDialog(context);
            await user.sendEmailVerification();
        } on PlatformException catch (e) {
            switch (e.code) {
                case "ERROR_EMAIL_AlREADY_IN_USE":
                  setState(() {
                      errorMsg = "This email is already in use.";
                  });
                  _showDialog1(context);
            }
            print(e.message);
        }
    }
},

Future<Void> _showDialog1(BuildContext context) {
    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Alert'),
          content: const Text('User Already Exists'),
          actions: <Widget>[
            FlatButton(
              child: Text('Ok'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => LoginPage(),
                  ),
                );
              },
            ),
          ],
        );
      },
    );
  }

对话框不工作,如果用户尝试注册并且不显示任何警报,则使用该对话框的用户将不知道发生了什么。

V/FA      (23130): Recording user engagement, ms: 67474
V/FA      (23130): Connecting to remote service
V/FA      (23130): Activity paused, time: 479832386
D/FA      (23130): Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=67474, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=8428444267785459928}]
V/FA      (23130): Connection attempt already in progress
D/FA      (23130): Connected to remote service
V/FA      (23130): Processing queued up service tasks: 2
V/FA      (23130): Inactivity, disconnecting from the service
V/FA      (23130): Connecting to remote service
V/FA      (23130): Activity resumed, time: 480570394
W/1.gpu   (23130): type=1400 audit(0.0:73052): avc: denied { search } for name="ctx" dev="debugfs" ino=15090 scontext=u:r:untrusted_app:s0:c153,c257,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
D/FA      (23130): Connected to remote service
V/FA      (23130): Processing queued up service tasks: 1
W/BiChannelGoogleApi(23130): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzak@5aa370
W/DynamiteModule(23130): Local module descriptor class for com.google.firebase.auth not found.
I/FirebaseAuth(23130): [FirebaseAuth:] Loading module via FirebaseOptions.
I/FirebaseAuth(23130): [FirebaseAuth:] Preparing to create service connection to gms implementation
I/flutter (23130): The email address is already in use by another account.
V/FA      (23130): Inactivity, disconnecting from the service
W/BiChannelGoogleApi(23130): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzak@5aa370
I/flutter (23130): The email address is already in use by another account

Flutter 中的 Catch 是否支持警报对话框,或者我必须为此尝试其他方法? V/FA (23130):记录用户参与度,毫秒:67474 V/FA (23130):连接到远程服务 V/FA (23130):活动暂停,时间:479832386 D/FA (23130): 记录事件 (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto,engagement_time_msec(_et)=67474, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=8428444267785459928}] V/FA (23130):连接尝试已在进行中 D/FA (23130):连接到远程服务 V/FA (23130):处理排队的服务任务:2 V/FA (23130):不活动,与服务断开连接 V/FA (23130):连接到远程服务 V/FA (23130):活动恢复,时间:48057039

【问题讨论】:

    标签: flutter


    【解决方案1】:

    只需更改您的代码如下

    switch (e.code) {
                            case "ERROR_EMAIL_ALREADY_IN_USE":
                              setState(() {
                                errorMsg = "This email is already in use.";
                              });
                              _showDialog1(context);
    
                          }
    

    你不能抛出一个对话框。它是一个小部件,您必须在屏幕上呈现它。所以只需调用您的警报对话框。

    你必须使用 Future 而不是 Future

    您可以使用的其他 firebase 例外是:

    - EMAIL_EXISTS
    - OPERATION_NOT_ALLOWED
    - TOO_MANY_ATTEMPTS_TRY_LATER
    - EMAIL_NOT_FOUND
    - INVALID_PASSWORD
    - USER_DISABLED
    

    【讨论】:

    • 即使在删除投掷后也没有显示警报。请提出任何替代解决方案
    • 请添加您的 _showDialog1() 方法。另外,在 switch case 中添加打印日志以检查它是否针对 platformException。
    • 请检查我是否添加了对话框和日志
    【解决方案2】:

    首先要把Future&lt;Void&gt;改成Future&lt;void&gt;,因为需要使用小写的void,否则有时候会报'Future&lt;void&gt;' is not a subtype of type 'Future&lt;Void&gt;'这样的错误

    现在,我尝试了您的代码并且它有效,您确定 e.codeERROR_EMAIL_AlREADY_IN_USE 吗?

    例如,ALREADY 字中除 'l' 外的所有字符都大写。

    另外,我向你推荐了error constants class 来解决这种情况。

    【讨论】:

      【解决方案3】:
      onPressed: () async {
                    if (_emailController.text.isEmpty ||
                        _passwordController.text.isEmpty) {
                      _showDialog2(context);
                    }
                    FirebaseUser user;
                    if (user == null) {
                      try {
                        user = await _firebaseAuth.createUserWithEmailAndPassword(
                            email: email, password: password);
                        _showDialog(context);
                        await user.sendEmailVerification();
                      } catch (e) {
                        _showDialog1(context);
                      }
                    }
                  },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多