whenValueNonNull<T> function

Future<T> whenValueNonNull<T>(
  1. ValueListenable<T> listenable, {
  2. Duration? timeout,
})

Implementation

Future<T> whenValueNonNull<T>(
  ValueListenable<T> listenable, {
  Duration? timeout,
}) {
  if (listenable.value != null) return Future.value(listenable.value);
  final completer = Completer<T>();
  void listener() {
    final value = listenable.value;
    if (value != null) {
      completer.complete(value);
      listenable.removeListener(listener);
    }
  }

  listenable.addListener(listener);

  if (timeout != null) {
    return completer.future.timeout(timeout);
  }
  return completer.future;
}