buildWidget method

Widget buildWidget({
  1. required Widget loading(),
  2. required Widget success(
    1. T data
    ),
  3. required Widget error(
    1. String message
    ),
  4. Widget empty()?,
  5. Widget networkError()?,
  6. Widget idle()?,
})

Build a Flutter widget directly from state – eliminates screen boilerplate.

  • loading Shown while the request is in-flight (required)
  • success Shown when data is available (required)
  • empty Shown when data is empty – defaults to loading shimmer if omitted (optional)
  • error Shown for failed / unknown error states (required)
  • networkError Shown for connectivity failures – defaults to error if omitted (optional)
  • idle Shown for idle state – defaults to SizedBox.shrink() if omitted (optional)

Example:

state.buildWidget(
  loading:      () => const GalleryShimmer(),
  success:      (items) => GalleryGrid(items),
  empty:        () => const EmptyGalleries(),
  error:        (msg) => ErrorView(msg, onRetry: _reload),
  networkError: () => NoInternetView(onRetry: _reload),
);

Implementation

Widget buildWidget({
  required Widget Function() loading,
  required Widget Function(T data) success,
  required Widget Function(String message) error,
  Widget Function()? empty,
  Widget Function()? networkError,
  Widget Function()? idle,
}) {
  return switch (this) {
    IdleState<T, E>() =>
    idle?.call() ?? const SizedBox.shrink(),

    LoadingState<T, E>() =>
        loading(),

    SuccessState<T, E>(data: final d) =>
        success(d),

    EmptyState<T, E>() =>
    empty?.call() ?? const SizedBox.shrink(),

    NetworkErrorState<T, E>(error: final e) =>
    networkError?.call() ?? error(e.message),

    FailedState<T, E>(error: final e) =>
        error(e.message),
  };
}