An alternative, if you want to extract a list of string from list of object, with selected or enabled flag.
To do that, you can remove objects with selected=false or something like name.length > 5, then do map.tolist to extract the list of string.
Usually i use this method to get filters to filtering search result. Example we have a products list with distance & country filters. We can simply use solution above and apply filter with selected true.
I don't speak english, hope you understand 😅.
class City {
const City(this.name, [this.selected= false]);
final String name;
final bool selected;
}
void main() {
final cities = [City('A'), City('B', true), City('C')];
cities.add(City('D', true));
/// Remove unselected items
cities.removeWhere((e) => !e.selected);
/// extract list of string from list of objects
final selectedCities = cities.map((c) => c.name).toList();
print(selectedCities);
}
print:
[B, D]