when method

Widget when({
  1. Widget none()?,
  2. Widget loading()?,
  3. Widget refresh(
    1. T data
    )?,
  4. Widget loaded(
    1. T data
    )?,
  5. Widget failed(
    1. Object error
    )?,
})

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)? failed,
}) {
  final widget = switch (status) {
    ServiceStatus.none => (none ?? loading)?.call(),
    ServiceStatus.loading => loading?.call(),
    ServiceStatus.refresh => (refresh ?? loaded)?.call(data),
    ServiceStatus.loaded => loaded?.call(data),
    ServiceStatus.failed => failed?.call(error),
  };

  if (widget == null) {
    throw UnimplementedError(
      "Service.when: Widget for $status is not implemented",
    );
  }

  return KeyedSubtree(key: ValueKey(status), child: widget);
}