complete method

void complete(
  1. [FutureOr<T>? value]
)

Completes operation with value.

If value is a Future the operation will complete with the result of that Future once it is available. In that case isCompleted will be true before the operation is complete.

If the type T is not nullable value may be not be omitted or null.

This method may not be called after either complete or completeError has been called once. The isCompleted is true when either of these methods have been called.

Implementation

void complete([FutureOr<T>? value]) {
  if (!_mayComplete) throw StateError('Operation already completed');
  _mayComplete = false;

  if (value is! Future<T>) {
    _completeNow()?.complete(value);
    return;
  }

  if (_inner == null) {
    // Make sure errors from [value] aren't top-leveled.
    value.ignore();
    return;
  }

  value.then((result) {
    _completeNow()?.complete(result);
  }, onError: (Object error, StackTrace stackTrace) {
    _completeNow()?.completeError(error, stackTrace);
  });
}