用户类别
class User {
final String uid;
final String username;
final String email;
final String photoUrl;
final String displayName;
final String bio;
User({
this.uid,
this.username,
this.email,
this.photoUrl,
this.displayName,
this.bio,
});
factory User.fromDocument(DocumentSnapshot doc) {
return User(
uid: doc['uid'],
email: doc['email'],
photoUrl: doc['photoUrl'],
displayName: doc['displayName'],
bio: doc['bio']
);
}
}
在Firestore中创建新用户的工作原理是
:createUserInFirestore() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
DocumentSnapshot doc = await usersRef.document(user.uid).get();
if(!doc.exists) {
final username = await Navigator.push(context, MaterialPageRoute(builder: (context) => CreateAccount(),));
usersRef.document(user.uid).setData({
"uid" : user.uid,
"username" : username,
"photoUrl" : user.photoUrl,
"email" : user.email,
"displayName" : username,
"bio" : "",
"timestamp" : timestamp
});
doc = await usersRef.document(user.uid).get();
}
currentUser = User.fromDocument(doc);
}
但是,无论出于何种原因都无法创建帖子-在出现空错误时调用ID:
createPostInFireStore({String mediaUrl, String description, String location}) async {
postsRef.document(widget.currentUser.uid).collection("userPosts").document(postId).setData({
"postId" : postId,
"ownerId" : widget.currentUser.uid,
"username": widget.currentUser.uid,
"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
);
captionController.clear();
locationController.clear();
}
我的问题是,当用户要发布图像时,它不会创建posts集合。它将图像存储在存储器中,但不创建集合。