callWithGuardedState<R> method

R callWithGuardedState<R>(
  1. R call()
)

Executes call preserving the internal drawing state.

See saveState and restoreState.

Implementation

R callWithGuardedState<R>(R Function() call) {
  saveState();

  var restored = false;

  try {
    var ret = call();

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