0

My .uid says: The getter 'uid' isn't defined for the type 'UserCredential'.

My Code:

 import 'package:firebase_auth/firebase_auth.dart';
 
 class AuthService {   final FirebaseAuth _firebaseAuth =
 FirebaseAuth.instance;
 
   Stream<String> get onAuthStateChanged =>
 _firebaseAuth.authStateChanges().map(
         (User user) => user?.uid,
       );
 
   Future<String>
 createUserWithEmailAndPassword(
       String email, String password, String name) async {
     final currentUser = await _firebaseAuth.createUserWithEmailAndPassword(
       email: email,
       password: password,
     );
 
     var updateInfo = UserUpdateInfo();
     await FirebaseAuth.instance.currentUser.updateProfile(displayName:name);
     await FirebaseAuth.instance.currentUser.reload();
     return FirebaseAuth.instance.currentUser.uid;   }
 
 Future<String>
 signInWithEmailAndPassword(
       String email, String password) async {
     return (await _firebaseAuth.signInWithEmailAndPassword(
             email: email, password: password))
         .uid;   }
 
   signOut() {
     return _firebaseAuth.signOut();   } }
 
 class UserUpdateInfo { }

How can I fix it? Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
danielalakin
  • 261
  • 1
  • 4
  • 6

1 Answers1

4

The signInWithEmailAndPassword and createUserWithEmailAndPassword methods return a UserCredential object, which (as the error message says) doesn't have a uid property. To get to the UID from the UserCredential, you do credential.user.uid.

You're looking for:

await (_firebaseAuth.signInWithEmailAndPassword(email: email, password: password)).user.uid

Or a bit more readable when spread over two lines/statements:

var credentials = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
return credentials.user.uid
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807