Issues with code deprecation saving user info to firebase database

Viewed 258

I have codes from former or deprecated flutter version on saving user info to firebase database. But i have been trying to upgraded the code to the latest firebase_auth package but 'FirebaseUser' is flagged as an error.

 void registerUser() async{

    final FirebaseUser user = (await _auth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text).catchError((ex){
//Check Error and display message
      PlatformException thisEx = ex;
      showSnackBar(thisEx.message);
    })).user;

// check if user registration is successful
    if(user != null){
      DatabaseReference newUserRef = FirebaseDatabase.instance.reference().child('users/${user.uid}');
  
      Map userMap = {
        'fullname': fullNameController.text,
        'email': emailController.text,
        'phone': phoneController.text,
      };
      newUserRef.set(userMap);
      Navigator.pushNamedAndRemoveUntil(context, MainPage.id, (route) => false);
      
    }

  }

I have tried upgrading the codes my self but, it doesn't look and behave like before. here is my code but i need some help. Thanks in advance...

  void registerUser() async{
    //final UserCredential user = (await _auth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text));
    try {
      UserCredential userCredential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(
        email: emailController.text,
        password: passwordController.text,
      );
      if (userCredential != null) {
        DatabaseReference newUserRef = FirebaseDatabase.instance.reference()
            .child('users/${userCredential.toString}');
        M

      }
    }
    on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        print('The password provided is too weak.');
      } else if (e.code == 'email-already-in-use') {
        print('The account already exists for that email.');
      }
    } catch (e) {
      print(e);
    }


  }
0 Answers
Related