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

AsyncNotifierConsumer<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. void dataListener(
    1. S data
    )?,
  5. VoidCallback? loadingListener,
  6. void errorListener(
    1. ErrorState error
    )?,
  7. bool buildWhen(
    1. S? previous,
    2. S? current
    )?,
  8. bool listenWhen(
    1. S? previous,
    2. S? current
    )?,
  9. void onInit(
    1. N notifier
    )?,
  10. 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.

dataListener, loadingListener, and errorListener are optional callbacks invoked for side effects when the state is BaseAsyncState.data, BaseAsyncState.loading, or BaseAsyncState.error, respectively.

buildWhen is an optional predicate that determines whether to rebuild when the data changes. listenWhen is an optional predicate that determines whether to call listeners 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):

AsyncNotifierConsumer<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

AsyncNotifierConsumer.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,

  /// Optional callback for side effects when the state is [BaseAsyncState.data].
  final void Function(S data)? dataListener,

  /// Optional callback for side effects when the state is [BaseAsyncState.loading].
  final VoidCallback? loadingListener,

  /// Optional callback for side effects when the state is [BaseAsyncState.error].
  final void Function(ErrorState error)? errorListener,
  bool Function(S? previous, S? current)? buildWhen,
  bool Function(S? previous, S? current)? listenWhen,
  super.onInit,
  super.key,
}) : super(
        listener: (state) => state.when(
          error: (e) => errorListener?.call(e),
          loading: () => loadingListener?.call(),
          data: (data) => dataListener?.call(data),
        ),
        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,
        listenWhen: (previous, current) =>
            listenWhen?.call(previous.data, current.data) ?? true,
      );