-
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
timeout on a stream firstWhere
call doesn't work
#43566
Comments
Your problem is that your This behavior was introduced with Dart 2.0.0 and mentioned in the changelog:
There are also a warning in the API for the
https://api.dart.dev/stable/2.9.3/dart-io/sleep.html If you rewrite you code so it does not make use of Stream<int> data() async* {
int count = 1;
// counts from 1 to infinity in 1 second intervals
while (true) {
await Future<void>.delayed(const Duration(seconds: 1));
yield count;
print('Stream: $count');
count++;
}
}
void main() async {
// waits for the stream to emit a 5 and returns it or times out after 1 second and returns null
final found = await data()
.firstWhere((e) => e == 5)
.timeout(const Duration(seconds: 1), onTimeout: () => null);
print('found $found');
} You will then get the following output:
Note that the |
Working as intended. The (No opinion on the use of |
I'm trying to listen to a stream until I find a certain value or timeout after 1 second.
Here's a simple example:
Expected behavior
Listens to the stream for 1 second and times out with a null value (because the timeout is set to 1 second and it takes 5 seconds to get to 5).
Expected output:
found null
Actual Behavior
Waits for 5 seconds for the stream to emit 5, despite the
timeout()
setting.Actual output:
found 5
The text was updated successfully, but these errors were encountered: