0

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

Xuuan Thuc
  • 2,340
  • 1
  • 5
  • 22
INI HOOD
  • 57
  • 1
  • 5

2 Answers2

0

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
    }
  })
});
Xuuan Thuc
  • 2,340
  • 1
  • 5
  • 22
0

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

    });
Paul Kumchi
  • 211
  • 4
  • 7