waitFor<T> method Null safety

Future<T?> waitFor<T>(
  1. FutureOr<T>? condition(
      ),
    1. {dynamic matcher,
    2. Duration timeout = defaultTimeout,
    3. Duration interval = defaultInterval}
    )

    Waits until condition evaluates to a value that matches matcher or until timeout time has passed. If condition returns a Future, then uses the value of that Future rather than the value of condition.

    If the wait is successful, then the matching return value of condition is returned. Otherwise, if condition throws, then that exception is rethrown. If condition doesn't throw then an expect exception is thrown.

    Implementation

    Future<T?> waitFor<T>(FutureOr<T>? Function() condition,
        {matcher,
        Duration timeout = defaultTimeout,
        Duration interval = defaultInterval}) async {
      var mMatcher = matcher == null ? null : m.wrapMatcher(matcher);
      var endTime = now.add(timeout);
      while (true) {
        try {
          var value = await condition();
          if (mMatcher != null) {
            _matcherExpect(value, mMatcher);
          }
          return value;
        } catch (e) {
          if (now.isAfter(endTime)) {
            rethrow;
          } else {
            await sleep(interval);
          }
        }
      }
    }