In a firestore document I have an Array that holds a list of maps that have a valueId that is either true or false:
I am able to see if a valueId exists in the array with if(doc['inPack'].contains(valueID)){}
But now that I know it exists in the array, is there an easy way to grab if the specific valueID is true or false without iterating through the entire array? Or will I have to run a loop to grab the value?
Just trying to see if there is a more efficient way of doing this, thanks!
CodePudding user response:
If I understand your data structure, then you probably won't get the correct result using what you've presented...
You can use the collection
package to get the value
of the key
if it exists in a Map that is somewhere in the array as the following:
// data is of type List<Map<String, bool>>
final value = data.firstWhereOrNull((e) => e.containsKey('key1'))?['key1'];
Check this dartpad for a quick demo: https://dartpad.dev/f89e6ffa874295d9a7c7296db003c041