0

In a firestore document I have an Array that holds a list of maps that have a valueId that is either true or false:

enter image description here

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!

Ten Digit Grid
  • 1,315
  • 4
  • 22
  • 43

1 Answers1

1

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

Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30
  • SO I think a map is the better direection here for me, instead of the array thanks. But now I am havign issues adding items to the map. I keep just overwritting the single item in the maps... – Ten Digit Grid Aug 28 '22 at 20:32
  • 1
    Post a separate question for that :) – Robert Sandberg Aug 28 '22 at 20:40
  • I posted the new question here: https://stackoverflow.com/questions/73522154/how-to-add-or-edit-specific-value-within-a-map-on-a-firestore-document – Ten Digit Grid Aug 28 '22 at 20:48