obx method
Widget
obx(
- NotifierBuilder<
T> widget, { - Widget? onError(
- String? error
- Widget? onLoading,
- Widget? onEmpty,
- WidgetBuilder? onCustom,
- double? loadingIndicatorSize,
- AlignmentGeometry? alignment,
- String? emptyText,
- bool showLoading = true,
- bool showError = true,
- bool showEmpty = true,
- 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);
},
);
}