obx method
Widget
obx(
- NotifierBuilder<
T> widget, { - Widget onError(
- String? error
- Widget? onLoading,
- Widget? onEmpty,
- WidgetBuilder? onCustom,
Builds a widget based on the current state.
This method provides a convenient way to build widgets based on the state's status. It takes a NotifierBuilder function as a parameter, which defines the widget to be built based on the state's value.
Implementation
Widget obx(
NotifierBuilder<T> widget, {
Widget Function(String? error)? onError,
Widget? onLoading,
Widget? onEmpty,
WidgetBuilder? onCustom,
}) =>
Observer(
builder: (context) => switch (status) {
var s when s.isLoading =>
onLoading ?? const Center(child: CircularProgressIndicator()),
var s when s.isError => onError != null
? onError(s.errorMessage)
: Center(child: Text("An error occurred: ${s.errorMessage}")),
var s when s.isEmpty => onEmpty ?? const SizedBox.shrink(),
var s when s.isSuccess => widget(value),
var s when s.isCustom =>
onCustom?.call(context) ?? const SizedBox.shrink(),
_ => widget(value)
},
);