AsyncNotifierBuilder<N extends BaseNotifier<BaseAsyncState<S>>, S extends Object?>.withData constructor

AsyncNotifierBuilder<N extends BaseNotifier<BaseAsyncState<S>>, S extends Object?>.withData({
  1. required Widget loadingBuilder(
    1. Widget? child
    ),
  2. required BuilderCallback<S> dataBuilder,
  3. required Widget errorBuilder(
    1. ErrorState error,
    2. Widget? child
    ),
  4. bool buildWhen(
    1. S? previous,
    2. S? current
    )?,
  5. void onInit(
    1. N notifier
    )?,
  6. Key? key,
})

dataBuilder is called to build the widget tree when the state is BaseAsyncState.data. loadingBuilder is called when the state is BaseAsyncState.loading. errorBuilder is called when the state is BaseAsyncState.error.

buildWhen is an optional predicate that determines whether to rebuild when the data changes.

onInit is an optional callback invoked with the notifier when the widget is initialized.

Note: Do not manually call the notifier's onInit() method here. The notifier itself will automatically trigger its own onInit() when it is created, so calling it again would result in duplicate invocations.

Example (incorrect usage):

AsyncNotifierBuilder<MyAsyncNotifier, String>(
  // Do NOT do this:
  onInit: (notifier) => notifier.onInit(),
  dataBuilder: (data) => Text(data),
  loadingBuilder: () => CircularProgressIndicator(),
  errorBuilder: (error) => Text('Error: \\${error.message}'),
)

key is the widget key.

Implementation

AsyncNotifierBuilder.withData({
  /// Called when the state is [BaseAsyncState.loading].
  required final Widget Function(Widget? child) loadingBuilder,

  /// Called when the state is [BaseAsyncState.data].
  required final BuilderCallback<S> dataBuilder,

  /// Called when the state is [BaseAsyncState.error].
  required final Widget Function(ErrorState error, Widget? child)
      errorBuilder,
  final bool Function(S? previous, S? current)? buildWhen,
  super.onInit,
  super.key,
}) : super(
        builder: (state, child) => state.when(
          loading: () => loadingBuilder(child),
          data: (data) => dataBuilder(data, child),
          error: (error) => errorBuilder(error, child),
        ),
        buildWhen: (previous, current) =>
            buildWhen?.call(previous.data, current.data) ?? true,
      );