when method

Widget when({
  1. required Widget data(
    1. T data
    ),
  2. Widget loading()?,
  3. Widget error(
    1. Object error,
    2. VoidCallback retry
    )?,
  4. Widget idle()?,
  5. bool autoFetch = true,
  6. bool showStaleData = true,
})

Builds UI declaratively based on the query state.

A concise alternative to ZenQueryBuilder for common cases.

Example:

userQuery.when(
  data: (user) => UserCard(user),
  loading: () => const CircularProgressIndicator(),
  error: (e, retry) => ErrorView(e, onRetry: retry),
)

All parameters except data are optional. Default fallbacks are used when a builder is not provided:

  • error: A basic error message with a retry button
  • idle: SizedBox.shrink()

Implementation

Widget when({
  required Widget Function(T data) data,
  Widget Function()? loading,
  Widget Function(Object error, VoidCallback retry)? error,
  Widget Function()? idle,
  bool autoFetch = true,
  bool showStaleData = true,
}) {
  return ZenQueryBuilder<T>(
    query: this,
    builder: (context, value) => data(value),
    loading: loading,
    error: error,
    idle: idle,
    autoFetch: autoFetch,
    showStaleData: showStaleData,
  );
}