本文介绍了.uid:未为类型"UserCredential"定义吸气剂"uid".-Flutter,FirebaseAuth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.uid说:未为类型'UserCredential'定义吸气剂'uid'.

My .uid says: The getter 'uid' isn't defined for the type '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 { }

我该如何解决?谢谢!

推荐答案

signInWithEmailAndPassword 方法返回 UserCredential 对象,该对象(如错误消息所述)没有 uid 属性.要从 UserCredential 进入UID,请执行 credential.user.uid .

The signInWithEmailAndPassword method returns 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.

您正在寻找:

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

这篇关于.uid:未为类型"UserCredential"定义吸气剂"uid".-Flutter,FirebaseAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:14