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

问题描述

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

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

我该如何解决?谢谢!

推荐答案

signInWithEmailAndPasswordcreateUserWithEmailAndPassword 方法返回一个 UserCredential 对象,(如错误消息所述)没有 属性.要从 UserCredential 获取 UID,您需要执行 credential.user.uid.

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.

您正在寻找:

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"类型定义 getter“uid".- 颤振,FirebaseAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:14