builderData<R> method

Widget builderData<R>(
  1. Widget builder(
    1. R data
    ), {
  2. Widget onWaiting()?,
  3. Widget onError(
    1. dynamic error
    )?,
  4. void initState()?,
  5. void onSetState(
    1. SnapState<T?> state
    )?,
  6. void onSetStateData(
    1. R data
    )?,
  7. void onAfterBuild()?,
  8. void dispose()?,
  9. bool shouldRebuild(
    1. SnapState,
    2. SnapState
    )?,
  10. Object? watch()?,
  11. String? debugPrintWhenRebuild,
  12. String? tag,
})

Data builder handling waiting/error/data states uniformly.

Implementation

Widget builderData<R>(
  Widget Function(R data) builder, {
  Widget Function()? onWaiting,
  Widget Function(dynamic error)? onError,
  void Function()? initState,
  void Function(SnapState<T?> state)? onSetState,
  void Function(R data)? onSetStateData,
  void Function()? onAfterBuild,
  void Function()? dispose,
  bool Function(SnapState<dynamic>, SnapState<dynamic>)? shouldRebuild,
  Object? Function()? watch,
  String? debugPrintWhenRebuild,
  String? tag,
}) {
  SideEffects<T?>? sideEffects;
  if (initState != null ||
      onSetState != null ||
      onAfterBuild != null ||
      onSetStateData != null ||
      dispose != null) {
    void Function(SnapState<T?> state)? onBuilderSetState;
    if (onSetState != null || onSetStateData != null) {
      onBuilderSetState = (SnapState<T?> state) {
        onSetState?.call(state);
        if (onSetStateData != null) {
          final data = state.data;
          if (data is R || (data == null && null is R)) {
            onSetStateData(data as R);
          }
        }
      };
    }
    sideEffects = SideEffects<T?>(
      initState: initState,
      onSetState: onBuilderSetState,
      onAfterBuild: onAfterBuild,
      dispose: dispose,
    );
  }

  return OnBuilder<T?>(
    listenTo: this,
    sideEffects: sideEffects,
    shouldRebuild: (oldSnap, newSnap) {
      final currentTag = currentNotificationTag;
      if (tag != null && currentTag != null && tag != currentTag) {
        return false;
      }
      return shouldRebuild?.call(oldSnap, newSnap) ?? true;
    },
    watch: watch,
    debugPrintWhenRebuild: debugPrintWhenRebuild,
    builder: () {
      final state = snapState;
      if (state.isWaiting) {
        return onWaiting?.call() ?? _StaticBuilderWidgets.waitingWidget;
      }
      if (state.hasError) {
        return onError?.call(state.snapError) ??
            _StaticBuilderWidgets.getErrorWidget(state.snapError);
      }
      final data = state.data;
      if (data is R ||
          (data == null && null is R) ||
          (data != null && R == Object)) {
        return builder(data as R);
      }
      return _StaticBuilderWidgets.emptyWidget;
    },
  );
}