[英]add more value in the List from TextForm in flutter
我有這個 function:
Future<void> userProfile(String name, age, String codes) async {
String uid = _firebase.currentUser!.uid;
try {
Map<String, dynamic> map;
map = {
"name": name,
'age': age,
'codes' : [codes],
};
await _userCollection.doc(uid).update(map);
} catch (error) {
print('updateUserName error: $error');
}
}
在這一行:
'codes' : [codes],
我需要從 TextForm 向此列表添加一個值,它工作正常,但每次添加時只需替換值,我只需要在列表中添加另一個值而不是替換第一個值?
如果您使用的是 Firebase,我認為有一個 FieldValue 元素可以幫助您。
這是您使用該 FieldValue 的示例代碼
Future<void> userProfile(String name, age, String codes) async {
String uid = _firebase.currentUser!.uid;
try {
Map<String, dynamic> map;
map = {
"name": name,
'age': age,
'codes' : FieldValue.arrayUnion([codes]),
};
await _userCollection.doc(uid).update(map);
} catch (error) {
print('updateUserName error: $error');
}
}
這是該方法的文檔:
返回一個 FieldValue,告訴服務器將給定元素與服務器上已經存在的任何數組值聯合。
數組中尚不存在的每個指定元素都將添加到末尾。 如果被修改的字段還不是一個數組,它將被一個包含指定元素的數組覆蓋。
這應該 append 您的元素到列表中。 我沒有測試它,所以我希望它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.