createUserWithEmailAndPassword method

Future<UserCredential?> createUserWithEmailAndPassword({
  1. required String email,
  2. required String password,
  3. bool updateProfile = false,
  4. String? displayName,
  5. String? photoURL,
  6. dynamic onError(
    1. String
    )?,
})

CreateAccountWithEmailAndPassword method to login to existing account

Following error will be added to stream if conditions aren't met

  • Account already exists:
  • Thrown if there already exists an account with the given email address.
  • Provided email is invalid:
  • Thrown if the email address is not valid.
  • Error occured. Contact admin!!:
  • Thrown if email/password accounts are not enabled. Enable email/password accounts in the Firebase Console, under the Auth tab.
  • Provided password is weak:
  • Thrown if the password is not strong enough.

Implementation

Future<UserCredential?> createUserWithEmailAndPassword({
  required String email,
  required String password,
  bool updateProfile = false,
  String? displayName,
  String? photoURL,
  Function(String)? onError,
}) async {
  try {
    print(email);
    var creds = await _auth.createUserWithEmailAndPassword(
        email: email, password: password);
    if (creds.user != null) _errorStreamController.sink.add(null);
    if (updateProfile) {
      await creds.user?.updateDisplayName(displayName);
      await creds.user?.updatePhotoURL(photoURL);
    }
    return creds;
  } on FirebaseAuthException catch (e) {
    switch (e.code) {
      case 'email-already-in-use':
        _errorStreamController.sink.add('Account already exists');
        if (onError != null) onError('Account already exists');
        break;
      case 'invalid-email':
        _errorStreamController.sink.add('Provided email is invalid');
        if (onError != null) onError('Provided email is invalid');
        break;
      case 'weak-password':
        _errorStreamController.sink.add('Provided password is weak');
        if (onError != null) onError('Provided password is weak');
        break;
      default:
        _errorStreamController.add('Error occured. Contact admin!!');
        if (onError != null) onError('Error occured. Contact admin!!');
        break;
    }
  }
}