Uploading data type of Map or Array to Firestore database to match Firestore map and array type

7,250

I think this will work

Firestore.instance.collection('/mycollection').add({
  'mapData' : myMap,
}).then((onValue) {
  // do something
}).catchError((e) {
  print(e);
});
Share:
7,250
Uma
Author by

Uma

Updated on December 02, 2022

Comments

  • Uma
    Uma over 1 year

    I have a Map of type Map<String, String> and I am trying to upload it to Firestore to match the map datatype available as a field. See image below.

    I have tried using the toString method for the map class and assigning it to a field but it shows up as a long string rather than a map, see code snippet below

    Firestore.instance.collection('/mycollection').add({
      'mapData' : myMap.toString(), // myMap.toString() = {data: test, data2: name}
    }).then((onValue) {
      // do something
    }).catchError((e) {
      print(e);
    });
    

    The above snippet uploads the to string value of myMap as a string and assigns it to mapdata. It does not send the data in the map format to Firestore

    Any ideas on how to send a type of Map<string, string> to Firestore to map the type of map that Firestore provides ? And also from List to Firestore array

    • Doug Stevenson
      Doug Stevenson over 5 years
      I take it that you already tried to use myMap directly as the field value for mapData, and it didn't do what you expect?
    • Uma
      Uma over 5 years
      Nah I didnt actually, I just did and it worked.
  • Doug Stevenson
    Doug Stevenson over 5 years
    This will likely not create a field of type object as is shown in the screenshot. The document in the screenshot has a single field thisMap, and it contains an object with three properties.
  • dshukertjr
    dshukertjr over 5 years
    @DougStevenson You are right. Missed the small detail. Let me rethink
  • Uma
    Uma over 5 years
    You were right, that actually worked, thank you, it sent it to the Firestore as a map
  • user1689987
    user1689987 almost 3 years
    I don't see a Map object