A.A
A.A

Reputation: 4101

Dart ! operator after a future

What is the ! operator after _cachedValueFuture?

  Future<T> fetch(Future<T> Function() callback) async {
    if (_cachedStreamSplitter != null) {
      throw StateError('Previously used to cache via `fetchStream`');
    }
    if (_cachedValueFuture == null) {
      _cachedValueFuture = callback();
      await _cachedValueFuture;
      _startStaleTimer();
    }
    return _cachedValueFuture!;
  }

Upvotes: 0

Views: 51

Answers (1)

lrn
lrn

Reputation: 71693

It's the "(not-)null assertion operator" which becomes part of Dart with the Null Safety feature in the next release.

The _cachedValueFuture variable has type Future<T>?, which means a future or null. The ! operator throws if the value is null, so the type of _cachedValueFuture! is Future<T> which is the required return type of the function.

Upvotes: 2

Related Questions