obx method

Widget obx(
  1. NotifierBuilder<T> widget, {
  2. Widget? onError(
    1. String? error
    )?,
  3. Widget? onLoading,
  4. Widget? onEmpty,
  5. WidgetBuilder? onCustom,
  6. double? loadingIndicatorSize,
  7. AlignmentGeometry? alignment,
  8. String? emptyText,
  9. bool showLoading = true,
  10. bool showError = true,
  11. bool showEmpty = true,
  12. bool isStateless = false,
})

Implementation

Widget obx(
  NotifierBuilder<T> widget, {
  Widget? Function(String? error)? onError,
  Widget? onLoading,
  Widget? onEmpty,
  WidgetBuilder? onCustom,
  double? loadingIndicatorSize,
  AlignmentGeometry? alignment,
  String? emptyText,
  bool showLoading = true,
  bool showError = true,
  bool showEmpty = true,
  bool isStateless = false,
}) {
  if (isStateless) {
    showLoading = false;
    showError = false;
    showEmpty = false;
  }

  return Observer(
    builder: (context) {
      if (status.isLoading) {
        if (!showLoading) return const SizedBox.shrink();
        return onLoading ??
            _defaultLoading(size: loadingIndicatorSize, alignment: alignment);
      } else if (status.isError) {
        if (!showError) return const SizedBox.shrink();
        if (onError != null) {
          return onError(status.errorMessage) ?? const SizedBox.shrink();
        }
        return _defaultError(status.errorMessage);
      } else if (status.isEmpty) {
        if (!showEmpty) return const SizedBox.shrink();
        return onEmpty ?? _defaultEmpty(context, emptyText: emptyText);
        // Also can be widget(null); but is risky
      } else if (status.isSuccess) {
        return widget(value);
      } else if (status.isCustom) {
        return onCustom?.call(context) ??
            const SizedBox.shrink(); // Also can be widget(null); but is risky
      }
      return widget(value);
    },
  );
}