AsyncNotifierBuilder<N extends BaseNotifier<BaseAsyncState<S> > , S extends Object?>.withData constructor
AsyncNotifierBuilder<N extends BaseNotifier<BaseAsyncState<S> > , S extends Object?>.withData ({})
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() loadingBuilder,
/// Called when the state is [BaseAsyncState.data].
required final Widget Function(S state) dataBuilder,
/// Called when the state is [BaseAsyncState.error].
required final Widget Function(ErrorState error) errorBuilder,
final bool Function(S? previous, S? current)? buildWhen,
super.notifier,
super.onInit,
super.key,
}) : super(
builder: (state) => state.when(
data: dataBuilder,
error: errorBuilder,
loading: loadingBuilder,
),
buildWhen: (previous, current) =>
buildWhen?.call(previous.data, current.data) ?? true,
);