when method
Returns a widget depending on the current ServiceStatus.
Implementation
Widget when({
Widget Function()? none,
Widget Function()? loading,
Widget Function(T data)? refresh,
Widget Function(T data)? loaded,
Widget Function(Object error, StackTrace stackTrace)? failed,
}) {
final widget = switch (status) {
.none => (none ?? loading)?.call(),
.loading => loading?.call(),
.refresh => (refresh ?? loaded)?.call(data),
.loaded => loaded?.call(data),
.failed => failed?.call(error, stackTrace),
};
if (widget == null) {
throw UnimplementedError(
"Service.when: Widget for $status is not implemented",
);
}
return KeyedSubtree(key: ValueKey(status), child: widget);
}