.uid:未为“UserCredential”类型定义 getter“uid”。 - 颤振,FirebaseAuth

时间:2021-02-17 21:19:06

标签: firebase flutter firebase-authentication uid

我的 .uid 说:getter 'uid' 没有为类型 'UserCredential' 定义。

我的代码:

 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 { }

我该如何解决?谢谢!

1 个答案:

答案 0 :(得分:3)

signInWithEmailAndPassword 方法返回一个 UserCredential object,它(如错误消息所述)没有 uid 属性。要从 UserCredential 访问 UID,请执行 credential.user.uid

您正在寻找:

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

或者当分布在两行/语句上时更具可读性:

var credentials = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
return credentials.user.uid