obtainNewKey<S> method

Future<S> obtainNewKey<S>(
  1. S createNewKey(
    1. T value
    ),
  2. Future<T> obtainKey()
)

obtain new key base on old key

Implementation

Future<S> obtainNewKey<S>(
  S Function(T value) createNewKey,
  Future<T> Function() obtainKey,
) {
  Completer<S>? completer;
  Future<S>? result;

  obtainKey().then((T value) {
    final S key = createNewKey(value);
    if (completer != null) {
      // We already returned from this function, which means we are in the
      // asynchronous mode. Pass the value to the completer. The completer's
      // future is what we returned.
      completer.complete(key);
    } else {
      // We haven't yet returned, so we must have been called synchronously
      // just after loadStructuredData returned (which means it provided us
      // with a SynchronousFuture). Let's return a SynchronousFuture
      // ourselves.
      result = SynchronousFuture<S>(key);
    }
  });
  if (result != null) {
    return result!;
  }

  completer = Completer<S>();
  return completer.future;
}