-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
where
can't accept an async predicate
#56284
Comments
Summary: The |
There are already a solution for this if you import the package import 'package:stream_transform/stream_transform.dart';
void main() async {
Set<String> set = {'One', 'Two', 'Three'};
List<String> values = await Stream.fromIterable(set)
.asyncWhere((value) =>
Future.delayed(Duration(seconds: 1), () => value.length == 3))
.toList();
print(values); // [One, Two]
} We should get a |
I considered this solution, but I think it's a waste of resources, as this would create a
Not necessarily, in my case I want to filter a |
@feinstein But that removes a large part of using
I did also not suggest returning a If you really don't want to deal with Streams, I think it is better for you to just create a custom function (maybe extension on But I don't think your proposed design (having |
No plans to add async support on base collections, or other non-asynchronous constructrs. Asynchrony is something you opt in to, and then you choose where to put your asynchronous gaps, and how to combine asynchronous computations. In this case, you can do any of the following: Sequential checks using Set result = {for (var item in set) if (await repository.getCount(item.id) < item.maxCount) item} or Set result = {};
for (var item in set) {
if (await repository.getCount(item.id) < item.maxCount) result.add(item);
} Parallel checks using Future<Item?> ifValidCount(Item item) async =>
(await repository.getCount(item.id)) < item.maxCount ? item : null;
Set result = (await set.map(ifValidCount).wait).nonNulls.toSet(); Use a stream to opt in to asynchrony. Set result = await Stream.fromIterable(set).asyncWhere(
(item) async => (await repository.getCount(item.id)) < item.maxCount).toSet(); There are many options, but they are not members of |
Thanks for the thorough explanation, as always @lrhn. I was indeed using |
Consider this code:
This will raise an error, because
where
needs a predicate that returnsbool
and notFuture<bool>
. Other methods fromiterable.dart
have the same issue.I imagine one way of solving this is by creating a
whereAsync
that returns aFuture<Iterable>
instead.The text was updated successfully, but these errors were encountered: