I have a list List<Item> list
and a function Future<bool> myFilter(Item)
.
Is there a way to filter my list
using the Future returning function myFilter()
?
The idea is to be able to do something like this:
final result = list.where((item) => myFilter(item)).toList();
But this is not possible since where
expects bool
and not Future<bool>
Since the iteration involves async operation, you need to use a Future
to perform the iteration.
final result = <Item>[];
await Future.forEach(list, (Item item) async {
if (await myFilter(item)) {
result.add(item);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With