skip to Main Content

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'])
                              }),

Img: enter image description here

Update ‘counts’ of both users using userDocumentUid at the same time

enter image description here

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    .doc(json.encode(userDocumentId??[]))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search