![](/img/trans.png)
[英]Flutter : NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom
[英]flutter :NoSuchMethodError: The getter 'id' was called on null. Receiver: null Tried calling: id
當我注冊一個新用戶甚至登錄時,用戶的數據並沒有存儲在 currentUser 中,所以當我想用它來將帖子數據保存在一個名為 posts 的新集合中時,我會收到這個錯誤。NoSuchMethodError: The getter '在 null 上調用了 id'。 接收器:null 嘗試呼叫:id。 我也得到了這個異常: throw new NoSuchMethodError.withInvocation(this, invocation);
我的注冊碼:
Future<void> registerWithEmailAndPassword(
String email, String password, username) async {
print("registering now");
try {
final authResult = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
var _uid = authResult.user.uid;
// DocumentSnapshot doc = await usersRef.doc(user.id).get();
print("this is userid: $_uid");
createUserInFirestore(authResult, username);
return _userFromFirebase(authResult.user);
} catch (e) {
print(e.toString());
return null;
}
}
Future<void> logout() async {
await FirebaseAuth.instance.signOut();
}
Future<void>createUserInFirestore(authResult, username) async {
var User user= FirebaseAuth.instance.currentUser;
DocumentSnapshot doc = await usersRef.doc(user.uid).get();
if (!doc.exists) {
collection
usersRef.doc(user.uid).set({
"userid": user.uid,
"username": username,
"photoUrl": user.photoURL,
"email": user.email,
"displayName": user.displayName,
"bio": "",
"timestamp": timestamp
});
doc = await usersRef.doc(user.uid).get();
}
currentUser = Users.fromDocument(doc);
}
和她的職位
createPostInFirestore(
{String mediaUrl, String location, String description}) {
postsRef
.doc(widget.currentUser.id)
.collection("userPosts")
.doc(postId)
.set({
"postId": postId,
"ownerId": widget.currentUser.id,
"username": widget.currentUser.username,
"mediaUrl": mediaUrl,
"description": description,
"location": location,
"timestamp": timestamp,
"likes": {},
});
}
handleSubmit() async {
setState(() {
isUploading = true;
});
await compressImage();
String mediaUrl = await uploadImage(file);
createPostInFirestore(
mediaUrl: mediaUrl,
location: locationController.text,
description: captionController.text,
);
setState(() {
file = null;
isUploading = false;
postId = Uuid().v4();
});
}
我用這個 model 來保存 currentUser 數據
import 'package:cloud_firestore/cloud_firestore.dart';
class Users {
final String id;
final String username;
final String email;
final String photoUrl;
final String displayName;
final String bio;
Users({
this.id,
this.username,
this.email,
this.photoUrl,
this.displayName,
this.bio,
});
factory Users.fromDocument(DocumentSnapshot doc) {
return Users(
id: doc['userid'],
email: doc['email'],
username: doc['username'],
//photoUrl: doc['photoUrl'],
//displayName: doc['displayName'],
bio: doc['bio'],
);
}
}
請嘗試像這樣引用您當前的用戶:
//you were missing 'this'
this.widget.currentUser.id
並請提供更多信息。 這兩個功能是同一個 class 還是小部件? 如果是,它是有狀態的嗎?你打電話了嗎?
setState((){});
也許您必須等待注冊完成,所以您正在嘗試訪問 currentUser,即使他尚未設置?
您的錯誤表明您的 currentUser 變量是 null,因此您不能使用它。 我建議在執行任何涉及 POSTING 的代碼之前檢查 currentUser 是否為 null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.