Reputation: 386
I have two objects that look like this:
class Field extends RESTModel {
int id;
String name;
String dataType;
int objectId;
}
class FieldKeywordMap extends RESTModel {
int id;
String keywordName;
int objectConfigurationId;
int fieldId;
}
I have a list of FieldKeywordMaps called _fieldKeywordMaps and a list of Fields called _selectedFields. I am writing a function to return a filtered version of _fieldKeywordMaps based on 2 conditions:
Filtering based on the first condition works, but Im having trouble determining how to iterate through my _selectedFields list and compare it to the object in _fieldKeywordMaps. Here is the code:
selectObjectKeywordMaps() async {
if (selectedObjectConfiguration() != null && selectedObject() != null) {
List<FieldKeywordMap> _maps = _fieldKeywordMaps
.where((keywordMap) =>
keywordMap.objectConfigurationId == selectedObjectConfiguration().id)
.where((filteredKeywordMap) =>
_selectedFields.map((field) => field.id).contains(filteredKeywordMap.id))
.toList();
_selectedObjectKeywordMaps = _maps.toList();
fetchAffectedCustomers();
} else {
_selectedObjectKeywordMaps = [];
}
notifyListeners();
}
Upvotes: 2
Views: 4262
Reputation: 89965
.where((filteredKeywordMap) => _selectedFields.map(...))
is inappropriate. Iterable.map
is used when you want to perform a 1:1 transformation from one Iterable
to another.
You instead could do:
.where((filteredKeywordMap) => _selectedFields.any((field) => field.id == filteredKeywordMap.fieldId))
Note that that can be inefficient. If you made _selectedFields
a Map
from id
s to Field
s instead of being a List<Field>
s, that would make lookups faster and simpler.
Upvotes: 6