0

I'm trying to add an object to an array in Firestore. My code is

Future signUpForClass(String docId, Member member) async {
    await _fireStoreInstance.collection("collection_name").doc(docId).update({
      "signedUpMembers": FieldValue.arrayUnion([member])
    });
  }

but I get an error saying: Invalid argument: Instance of 'Member'

When I check on the empty array in Firestore it seems like the type of the array is a String and I am able to use a string argument, i.e. member.name, and then I can add/remove from the array. However, when I create the document with the "signedUpMembers" array, I pass the array an empty list of Members, i.e. "signedUpMembers": <Member>[]

What am I doing wrong? I've tried a bunch of different syntax for this but I haven't been able to add the whole object to the array, only strings.

3

1 Answer 1

1

Firebase Firestore only allows certain data-types (String, List, Map, GeoPoint, etc). To store your data, you should convert it to json/map.

class Member {

  String name;

  ...

  Map toJSON() {
    return {
      'name': name,
      ...
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.