postFrameRun<T> method

PostFrameTask<T> postFrameRun<T>(
  1. FutureOr<T> action(), {
  2. List<ScrollController> scrollControllers = const [],
  3. int maxWaitFrames = 5,
  4. bool waitForEndOfFrame = true,
  5. int endOfFramePasses = 2,
  6. Duration? timeout,
  7. PostFramePredicate? predicate,
  8. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Advanced post-frame run with context-aware mounted check predicate.

Automatically includes a predicate that checks if the widget is still mounted before executing the action. You can provide an additional predicate that will be AND-ed with the mounted check.

Implementation

PostFrameTask<T> postFrameRun<T>(
  FutureOr<T> Function() action, {
  List<ScrollController> scrollControllers = const [],
  int maxWaitFrames = 5,
  bool waitForEndOfFrame = true,
  int endOfFramePasses = 2,
  Duration? timeout,
  PostFramePredicate? predicate,
  void Function(Object error, StackTrace stackTrace)? onError,
}) {
  // Capture the element for mounted check.
  final element = this as Element;

  return PostFrame.run<T>(
    action,
    scrollControllers: scrollControllers,
    maxWaitFrames: maxWaitFrames,
    waitForEndOfFrame: waitForEndOfFrame,
    endOfFramePasses: endOfFramePasses,
    timeout: timeout,
    predicate: () {
      // Check if widget is still mounted.
      if (!element.mounted) return false;
      // Apply additional predicate if provided.
      return predicate?.call() ?? true;
    },
    onError: onError,
  );
}