如何在flutter中保存数据到Firestore



这是我已经使用的代码,但它没有在Firestore中保存我的数据。请帮助

Future<void>saveRestaurantDataToDb({String url, String restaurantName, String mobileNumber, String dialog}){
User user = FirebaseAuth.instance.currentUser;
DocumentReference _restaurants = FirebaseFirestore.instance.collection('restaurants').doc(user.uid);
_restaurants.set({
'uid':user.uid,
'restaurantName': restaurantName,
'mobile': mobileNumber,
'email': this.email,
'dialog': dialog,
'address': '${this.placeName}:${this.restaurantAddress}',
'location': GeoPoint(this.restaurantLatitude, this.restaurantLongitude),
'restaurantOpen': true, // will use latter
'rating': 0.00, //later
'totalRating': 0, //later
'isTopPicked': true, //later
'imageUrl': url,
});
return null;
}


尝试用CollectionReference代替Documentreference

CollectionReference users = FirebaseFirestore.instance.collection('users');
Future<void> addUser() {
return users
.doc('ABC123')
.set({
'full_name': "Mary Jane",
'age': 18
})
.then((value) => print("User Added"))
.catchError((error) => print("Failed to add user: $error"));
}
DocumentReference _restaurants = await FirebaseFirestore.instance.collection('restaurants').doc(user.uid); //add await here 

和异步到你的函数

void postingdata() async {
DocumentReference reference =
FirebaseFirestore.instance.collection('CartHistory').doc('${widget.name}');
await reference.set({
"total count": widget.total_Count,
"Name ": widget.name,
"total sum": widget.previciousSum
});
return null;
}

最新更新