简体   繁体   中英

get a map value by key from a list of maps in dart flutter

I need to get the value of a data when the name matches the key in a list of maps. Something like this:

List<Map> dataList = [];
await friends.doc("+2348******").collection("Friends").get().then((value) {
  for (var element in value.docs) {
    dataList.add(element.data());
  }
  print("${dataList.where((element) => element == "+23789900")}");
});

I expect this to print out: John doe I found something similar but they didnt solve my problem

Try this example:

List<Map> dataList = [];
await friends.doc("+2348******").collection("Friends").get().then((value) {
  for (var element in value.docs) {
    dataList.add(element.data());
  }
  dataList.forEach((element){
    if(element['name'] == "+2348******"){
      //print
    }
  })
});

I think you should try this code... Hopefully, it goes well... works with normal map at the moment.

List<Map> dataList = [];
    await friends.doc("+2348******").collection("Friends").get().then((value) {
      for (var element in value.docs) {
        dataList.add(element.data());
      }

    //Try this code
      var key = dataList.keys.firstWhere((k)
          => countries[k] == '+23789900', orElse: () => null);

    print(key); //output: 35

    });

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