Home > Software engineering >  error : display a red line under this statement _userFromFirebaseUser(userCredential.user);
error : display a red line under this statement _userFromFirebaseUser(userCredential.user);

Time:03-10

hi every one I'm trying making my app with flutter . it is contains a sign up page so I write the code and I'm retrieving an information from the firebase after I upload it .

the below code is the sign up code

UserModel? _userFromFirebaseUser(User userCredential) {
    return userCredential != null
        ? UserModel(
            id: userCredential.uid,
            bannerImageUrl: '',
            name: '',
            email: '',
            profileImageUrl: '')
        : null;
  }

  Stream<UserModel?> get userCredential {
    return auth
        .authStateChanges()
        .map((userCredential) => _userFromFirebaseUser(userCredential!));
  }

  Future SignUp(email, password) async {
    var formdata = formstate.currentState;

    if (formdata!.validate()) {
      print("valid");
      formdata.save();
      try {
        UserCredential userCredential =
            (await auth.createUserWithEmailAndPassword(
                email: myemail!, password: mypassword!));

        FirebaseFirestore.instance
            .collection('Users')
            .doc(userCredential.user!.uid)
            .set({'name': email, 'email': email});
        _userFromFirebaseUser(userCredential.user);

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

and the red line appears on this line _userFromFirebaseUser(userCredential.user); as it is appears in the picture . please help me

enter image description here

CodePudding user response:

userCredential.user      is User? not User

change the method parameter

UserModel? _userFromFirebaseUser(User? userCredential) {
    return ....
  }
  • Related