createOnce<T extends Object> function

T createOnce<T extends Object>(
  1. T factoryFunc(), {
  2. void dispose(
    1. T
    )?,
})

createOnce creates an object with the factory function factoryFunc at the time of the first build and disposes it when the widget is disposed if the object implements the Disposable interface. on every rebuild the same object is returned dispose allows you to pass a custom dispose function to dispose of the object. if provided it will override the default dispose behavior.

If can be used to create AnimationControllers or other objects that should be live for the lifetime of the widget.

Implementation

T createOnce<T extends Object>(
  T Function() factoryFunc, {
  void Function(T)? dispose,
}) {
  assert(
      _activeWatchItState != null,
      'createOnce can only be called inside a build function within a WatchingWidget'
      ' or a widget using the WatchItMixin');

  return _activeWatchItState!.createOnce(factoryFunc, dispose: dispose);
}