future<V> function

Widget future<V>(
  1. BuildContext context, {
  2. FutureOr<V?> waitFor,
  3. String? observerName,
  4. bool sliver = false,
  5. bool allowNull = false,
  6. V? initialData,
  7. Duration? timeout,
  8. String? key,
  9. required Widget builder(
    1. BuildContext context,
    2. V? resolved
    ),
  10. Widget errorBuilder(
    1. BuildContext context,
    2. dynamic error
    )?,
  11. Widget spinner(
    1. BuildContext context
    )?,
})

Shorthand for FutureBuilder - combines an Observer

Implementation

Widget future<V>(BuildContext context,
    {FutureOr<V?> waitFor,
    String? observerName,
    bool sliver = false,
    bool allowNull = false,
    V? initialData,
    Duration? timeout,
    String? key,
    required Widget builder(BuildContext context, V? resolved),
    Widget errorBuilder(BuildContext context, error)?,
    Widget spinner(BuildContext context)?}) {
  timeout ??= 20.seconds;
  errorBuilder ??= ((context, err) =>
      sliverBox(ErrorText(err?.toString() ?? "Error"), sliver));
  spinner ??= (context) =>
      sliverBox(Center(child: PlatformCircularProgressIndicator()), sliver);

  initialData ??= (waitFor is V) ? waitFor : null;
  var future = Future.value(waitFor);
  if (initialData == null) future = future.timeout(timeout);
  WidgetBuilder built = (_) => FutureBuilder<V?>(
      key: key == null ? null : Key("$key-future"),
      future: future,
      initialData: initialData,
      builder: (context, snapshot) {
        if (snapshot.hasData || allowNull == true) {
          return builder(context, snapshot.data);
        } else if (snapshot.hasError) {
          _log.severe("Error in future widget: $key: ${snapshot.error}",
              snapshot.error);
          return errorBuilder!(context, snapshot.error);
        } else {
          return spinner!(context);
        }
      });
  return observerName != null
      ? observe(context, observerName, built)
      : built(context);
}