
In this quick tutorial, I want to show you how to append data to an existing list using Firebase Storage. Let’s assume that you are with flutter firebase, and you have a token field, and there is stored a user’s device token. The way of appending the data to an existing field is using FieldValue.arrayUnion().
Basically, FieldValue.arrayUnion() is taking existing data from a firebase storage and unite it with your input. If the value exists in the firebase storage field array, it will skip the value.
Solution: Use FieldValue.arrayUnion()
Usage:
FieldValue.arrayUnion([token])
Here is a simple example of how to use FieldValue.arrayUnion() assuming that we want to update field tokens from a user reference:
String _uid = 'USER_UNIQUE_IDENTIFIER';
String _token = 'u7FeWMSN4WC6B2861E8oIdhW35UW7m4i';
static Future<void> updateToken(String uid, String token) async {
return await _userRef
.doc(uid)
.update(
{
'tokens': FieldValue.arrayUnion([token]),
},
);
}
await updateToken(_uid, _token);
I hope this quick tutorial helped you! Good luck with your development career!