Firebase 颤振-成功登录和注册后无法从Firestore检索数据
因此,我想在登录/成功注册后,在我的主屏幕上显示我的应用程序当前用户的名称 用户数据被正确地存储在firebase cloud Firestore中,但当我尝试检索它时,它显示getter被调用为“null” 以下是我得到的错误: 以下NoSuchMethodError被抛出构建主目录(脏, 依赖项:[MediaQuery,\u InheritedProviderScope], 州:#家乡#0a2b0): 对null调用了getter“name” 收件人:空 尝试呼叫:name 以下是相关的代码片段: 主屏幕构建方法:Firebase 颤振-成功登录和注册后无法从Firestore检索数据,firebase,flutter,authentication,google-cloud-firestore,Firebase,Flutter,Authentication,Google Cloud Firestore,因此,我想在登录/成功注册后,在我的主屏幕上显示我的应用程序当前用户的名称 用户数据被正确地存储在firebase cloud Firestore中,但当我尝试检索它时,它显示getter被调用为“null” 以下是我得到的错误: 以下NoSuchMethodError被抛出构建主目录(脏, 依赖项:[MediaQuery,\u InheritedProviderScope], 州:#家乡#0a2b0): 对null调用了getter“name” 收件人:空 尝试呼叫:name 以下是相关的代码
final userAuthProvider = Provider.of<UserAuthProvider>(context);
UserAuthProvider类相关代码:
enum Status { Uninitialized, Unauthenticated, Authenticating, Authenticated }
UserAuthProvider.initialize() : _auth = FirebaseAuth.instance {
_auth.authStateChanges().listen(_onStateChanged);
}
Future<void> _onStateChanged(User firebaseUser) async {
if (firebaseUser == null) {
_status = Status.Uninitialized;
} else {
_user = firebaseUser;
_status = Status.Authenticated;
_userModel = await _userServices.getUserById(firebaseUser.uid);
}
notifyListeners();
}
没有引用“userModel”,但我认为它是一个getter,看起来像:
UserModel get userModel => _userModel;
对吧??如果是这种情况,请尝试按如下方式更新您的getByUserId:
Future<UserModel> getUserById(String id) =>
UserModel.fromSnapshot(await _firebaseFirestore.collection(collection).doc(id).get());
Future-getUserById(字符串id)=>
UserModel.fromSnapshot(wait_firebaseFirestore.collection(collection.doc(id.get());
我找到了这个问题的解决方案
检索数据的正确方法是
UserModel.fromSnapshot(DocumentSnapshot snapshot) {
_name = snapshot['name'];
_phoneNumber = snapshot['phoneNumber'];
_email = snapshot['email'];
_password = snapshot['password'];
_id = snapshot['id'];
}
是的,正如您提到的,UserModel有一个getter,但是您的解决方案不起作用。我希望有其他想法!谢谢
UserModel get userModel => _userModel;
Future<UserModel> getUserById(String id) =>
UserModel.fromSnapshot(await _firebaseFirestore.collection(collection).doc(id).get());
UserModel.fromSnapshot(DocumentSnapshot snapshot) {
_name = snapshot['name'];
_phoneNumber = snapshot['phoneNumber'];
_email = snapshot['email'];
_password = snapshot['password'];
_id = snapshot['id'];
}