I have the problem with updating the documentUid as in array at the same time which I get a snapShot from here
var userDocumentUid = snapshot.data?["membersUid"];
But it says list’ can’t be assigned to the parameter type ‘string?’. Here .doc(userDocumentUid??[])
Full code:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("groups")
.doc(groupId)
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
//get array snapShot as show in img๐๐ป
var userDocumentUid = snapshot.data?["membersUid"];
return...
ElevatedButton(
onPressed: () async => await firestore
.collection("users")
//problem here๐๐ป
.doc(userDocumentUid??[])
.update({
'counts': FieldValue.arrayUnion(['1'])
}),
Update ‘counts’ of both users using userDocumentUid at the same time
2
Answers
Firstly, doc() requires a String value and you’re passing whole array i.e. userDocumentUid. You should pass with index like userDocumentUid[index].
Secondly, You’re using null aware operator in doc(userDocumentUid??[]) and passing an empty array if userDocumentUid[index] is null.. That’s wrong. You should make sure to pass a non-nullable userDocumentUid[index]. if you replace [] with "", this error will no come again but will come something like no document to update if userDocumentUid[index] is null.
please check whether userDocumentId(list) should be passed as a json or not.
because json.encode(anything) gives you string and we can pass it as json like