Reputation: 2861
i have this 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');
}
}
in this line :
'codes' : [codes],
i need to add a value to this List from TextForm, it is work fine, but just replace the value everytime i added, i need just to add another value in the list not replace the first one?
Upvotes: 0
Views: 22
Reputation: 761
If you are using Firebase I think there is a FieldValue element that helps you with that.
This is your example code 👨💻 with that 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');
}
}
This is the documentation 📝 of that method:
Returns a FieldValue that tells the server to union the given elements with any array value that already exists on the server.
Each specified element that doesn't already exist in the array will be added to the end. If the field being modified is not already an array it will be overwritten with an array containing exactly the specified elements.
This should append your element to the list. I didn't test it so I hope it works 😀.
Upvotes: 1