Storing List into Firestore In Multiple Map

Viewed 88

Hi I wanted to store a List into Firestore. I am working on a project and in this page I will need to select some selection (selection is the DocumentID) and store it in a List, After that I need to retrieve from the List and call Firestore again to get the GeoPoint of the item based on the List (the List contains the DocumentID). Then I am able to retrieve that as well thanks to the help of the community. But now I can only store one of the Item of the List and not all of them.

This is my code:

  void initRoute(data, dataInt) async{
    Firestore.instance.collection('routes').document(inputName).setData({
      dataInt:{
        "Lat" : data['location'].latitude,
        "Lng" : data['location'].longitude
      }
    });
  }

I am able to store but it only store the first Item in the List into the Firestore. How can i store all of them into just 1 Document with few Map something similar to the picture below.

And also the name of the Map can I generate it based on how many items I have in the list (1st item get 0, 2nd item get 1 and so on...)and add it in when storing? Thank you so much in advance.

Image

Edit: The dataInt is meant to do the numbering but I failed miserably but still do not know how to get it work.

Update: I am getting all the List from Firestore. Firstly the we select some from here: first

Then the after selecting it will store the Name into the List

Code of using the List:

List<String> _selectedBusStop = List<String>();

Code of adding to List when onTap and onLongPress

onTap: (){
            if(_selectedBusStop.contains(user.data['BusstopName'])){
              setState(() {
                _selectedBusStop.removeWhere((val) => val == user.data['BusstopName']);
              });
            }
          },
          onLongPress: (){
              setState(() {
                _selectedBusStop.add(user.data['BusstopName']);
              });
          }

Because the List only have the BusstopName saved in it so I will need to read again from Firestore and get the GeoPoint that I needed. Saved it in a Map and use it as a Polyline. Now I am currently stuck at where I am unable to save all of the Item in the List into the Firestore when I am calling the setData as it only save 1 of the item in the List and I want to store all of it.

Sorry for causing misunderstanding again. Really sorry for that.

Update I am able to store all of the Item in my List into Firestore

  void initRoute(data, dataInt) async{
    Firestore.instance.collection('routes').document(inputName).setData({
      data['BusstopName']:{
        "Lat" : data['location'].latitude,
        "Lng" : data['location'].longitude
      }
    }, merge: true);
  }

with this code.

1 Answers

First create a Map locally, give it the int you want:

Map<String,dynamic> dataToBeSent ={ 
       '0': {
        "Lat" : data['location'].latitude,
        "Lng" : data['location'].longitude}
        }

Now, before sending to firebase, you know if this is the first LatLng or not, if it's the first, you use .set, like this:

Firestore.instance.collection('routes').document(inputName).setData(dataToBeSent);

This will create a new document, with the coordinates inside a Map and it has the index you want. Later on, you want to add another LatLng to this document, you use .update, and send the new Map.

Map<String,dynamic> dataToBeSent2 ={ 
       '2': {
        "Lat" : data['location'].latitude,
        "Lng" : data['location'].longitude}
        }

Firestore.instance.collection('routes').document(inputName).updateData(dataToBeSent2);

This will keep updating the keys you are sending, but be careful to not store a new map using the same id, for example 0, it will over write what is currently at key 0 in firebase.


Based on your edits and comment replies, first start with this:

  void initRoute(data, dataInt) async{
    if(data.isNotEmpty){
      for(int a = 0; a < data; a++){
        Map<String,dynamic> dataToBeSent={
          $a:{
            "Lat" : data['location'].latitude,
            "Lng" : data['location'].longitude
          }
        };
        Firestore.instance.collection('routes').document(inputName).setData(dataToBeSent);
      }
    }
  }
Related