callWithGuardedStateAsync<R> method

FutureOr<R> callWithGuardedStateAsync<R>(
  1. FutureOr<R> call()
)

Executes call preserving the internal drawing state, accepting Future as return value.

See saveState and restoreState.

Implementation

FutureOr<R> callWithGuardedStateAsync<R>(FutureOr<R> Function() call) {
  saveState();

  var restored = false;

  try {
    var ret = call();

    if (ret is Future<R>) {
      return ret.whenComplete(() {
        restored = true;
        restoreState();
      });
    } else {
      restored = true;
      restoreState();
      return ret;
    }
  } finally {
    if (!restored) {
      restoreState();
    }
  }
}