如何在Flutter中处理Firebase Auth未处理的异常?

时间:2019-03-04 12:59:58

标签: dart flutter firebase-authentication

当我的应用程序的用户已经通过google注册后尝试通过Facebook进行注册时,Flutter会从控制台发出此错误。如何在代码中处理此错误?我假设输出的exception部分是错误代码?

Unhandled Exception: PlatformException(exception, An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address., null)
E/flutter ( 5581): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)

这是我的代码:

void loginWithFb() async {
    var auth = AuthProvider.of(context).auth;
    print('Signing up with fb...');
    setState(() {
      _showProgressIndicator = true;
    });

    FirebaseUser user = await auth.signInWithFBAcc();
    uId = user?.uid;


    if(uId != null) {
      print('Signed in: $uId');
      widget.onSignedIn(user);
    } else {

      print('fb login cancelled');

    }

//    _showAlert(context);
    setState(() {
      _showProgressIndicator = false;
    });
  }

1 个答案:

答案 0 :(得分:-1)

Future<String> signUp(String email, String password) async {
    try {
      FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      return user.uid;
    } on PlatformException catch (e) {
      print(e.toString());
      return e.code;
    }
  }