Dart: How do you make a Future wait for a Stream?

You can use the Stream method firstWhere to create a future that resolves when your Stream emits a true value.

Future<bool> whenTrue(Stream<bool> source) {
  return source.firstWhere((bool item) => item);
}

An alternative implementation without the stream method could use the await for syntax on the Stream.

Future<bool> whenTrue(Stream<bool> source) async {
  await for (bool value in source) {
    if (value) {
      return value;
    }
  }
  // stream exited without a true value, maybe return an exception.
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)