Tripping
Tripping

Reputation: 453

Flutter- How can i set a list of data to firestore

how can I set the list that I got to firestore

this is the list {001121804: true, 001121821: false, 001121838: true} what I want to do in firestorm is to set the id as a field and the boolean as a value

this is what the firestorm would look like when I set the data

users - newusers - 001121804: true
                   001121821: false
                   001121838: true

and this is the code

onPressed: () {
                      switchStates.forEach((key, value) {
                        keys.add(key);
                        values.add(value);
                      });
                      print(switchStates);
                      markAttendance();
                    }),

Upvotes: 0

Views: 387

Answers (1)

Edeson Bizerril
Edeson Bizerril

Reputation: 1715

The firestore call will be as follows.

FirebaseFirestore.instance.collection("users").doc("newusers").set({
      "001121804": true,
      "001121821": false,
      "001121838": true,
    });

Remembering that, as I understand it, you will have to call the values of your lists as follows:

List<int> keys = [001121804, 001121821, 001121838];
List<bool> values = [true, false, true];

Map<String, dynamic> resultMap = {};

for (var i = 0; i < keys.length; i++) {
  resultMap["${keys[i]}"] = values[i];
}

FirebaseFirestore.instance.collection("users").doc("newusers").set(resultMap);

Upvotes: 1

Related Questions