obx method

Widget obx(
  1. NotifierBuilder<T> widget, {
  2. Widget onError(
    1. String? error
    )?,
  3. Widget? onLoading,
  4. Widget? onEmpty,
  5. 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) {
        if (status.isLoading) {
          return onLoading ??
              const Center(child: CircularProgressIndicator());
        } else if (status.isError) {
          return onError != null
              ? onError(status.errorMessage)
              : Center(
                  child: Text("An error occurred: ${status.errorMessage}"),
                );
        } else if (status.isEmpty) {
          return onEmpty ?? const SizedBox.shrink();
        } else if (status.isSuccess) {
          return widget(value);
        } else if (status.isCustom) {
          return onCustom?.call(context) ?? const SizedBox.shrink();
        }
        return widget(value);
      },
    );