简体   繁体   中英

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();
                    }),

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM