嘿伙計們,所以我需要從 firestore 獲取一個欄位值以便在我的代碼中使用它,出于某種原因,我創建了這個方法來為我獲取值,但它總是回傳“未來”的實體,即使我的欄位是回傳是一個字串,任何人都知道這里有什么問題
getTheNextUserId() async{
var collection = FirebaseFirestore.instance.collection('collId');
var docSnapshot = await collection.doc('dId').get();
if (docSnapshot.exists) {
Map<String, dynamic> data = docSnapshot.data()!;
var uId = data['nextUserId'];
return uId;
}
Future addUserDetails(String firstName, String lastName, int age,String email) async {
final CollectionReference userRef = FirebaseFirestore.instance.collection('user');
await userRef.doc(getTheNextUserId.toString()).set({
'first name': firstName,
'last name': lastName,
'age': age,
'email': email,
'count': 10,});
}
uj5u.com熱心網友回復:
將您的 getTheNextUserId 更新為:
Future<String> getTheNextUserId {
...
}
然后在 addUserDetails 中執行此操作:
var str = await getTheNextUserId();
await userRef.doc(str).set({
'first name': firstName,
'last name': lastName,
'age': age,
'email': email,
'count': 10,});
uj5u.com熱心網友回復:
你getTheNextUserId是一個async函式,所以它也必須等待:
Future addUserDetails(String firstName, String lastName, int age,String email) async {
final CollectionReference userRef = FirebaseFirestore.instance.collection('user');
await userRef.doc((await getTheNextUserId()).toString()).set({ // in this line
'first name': firstName,
'last name': lastName,
'age': age,
'email': email,
'count': 10,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505225.html
標籤:Google Cloud Collective 扑 火力基地 镖 谷歌云火库
上一篇:僅在重新渲染后設定狀態
