我想在我的Catch块中添加一个警报对话框

时间:2019-10-15 04:43:19

标签: flutter

此代码用于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),捆绑包[{firebase_event_origin(_o)=自动,engage_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

3 个答案:

答案 0 :(得分:2)

只需如下更改代码

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

答案 1 :(得分:0)

首先,将Future<Void>更改为Future<void>,因为您需要使用小写的void,否则有时会出现类似'Future<void>' is not a subtype of type 'Future<Void>'的错误

现在,我已经尝试过您的代码,并且可以正常工作,确定e.codeERROR_EMAIL_AlREADY_IN_USE吗?

例如,在已存在的单词中,除“ l”外的所有字符均大写。

此外,出于这种情况,我向您建议了error constants class

答案 2 :(得分:0)

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);
                }
              }
            },