obx method

Widget obx(
  1. Widget onSuccess(
    1. T data
    ), {
  2. Widget onError(
    1. Object? error
    )?,
  3. Widget onLoading()?,
  4. Widget onInitial()?,
})

A cleaner alternative to handling UI logic explicitly. Similar to GetX's .obx mixin!

Implementation

Widget obx(
  Widget Function(T data) onSuccess, {
  Widget Function(Object? error)? onError,
  Widget Function()? onLoading,
  Widget Function()? onInitial,
}) {
  PodTracker.reportRead(this); // Auto-track changes inside `obx()`!

  switch (_status) {
    case AsyncStatus.initial:
      return onInitial?.call() ?? const SizedBox.shrink();
    case AsyncStatus.loading:
      return onLoading?.call() ??
          const Center(child: CircularProgressIndicator());
    case AsyncStatus.error:
      return onError?.call(_error) ??
          Center(
              child: Text("Error: $_error",
                  style: const TextStyle(color: Color(0xFFE53935))));
    case AsyncStatus.success:
      return onSuccess(value as T);
  }
}