buildWidget method
Build a Flutter widget directly from state – eliminates screen boilerplate.
loadingShown while the request is in-flight (required)successShown when data is available (required)emptyShown when data is empty – defaults toloadingshimmer if omitted (optional)errorShown for failed / unknown error states (required)networkErrorShown for connectivity failures – defaults toerrorif omitted (optional)idleShown 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),
};
}