AsyncNotifierConsumer<N extends BaseNotifier<BaseAsyncState<S> > , S extends Object?>.withData constructor
- required Widget loadingBuilder(
- Widget? child
- required BuilderCallback<
S> dataBuilder, - required Widget errorBuilder(
- ErrorState error,
- Widget? child
- void dataListener(
- S data
- VoidCallback? loadingListener,
- void errorListener(
- ErrorState error
- bool buildWhen(
- S? previous,
- S? current
- bool listenWhen(
- S? previous,
- S? current
- void onInit(
- N notifier
- 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,
);