builder method

Widget builder(
  1. KType recordId, {
  2. RecordDataServiceWidgetBuilderWithContext<X?, KType>? builder,
  3. WidgetDataBuilder<X>? successFn,
  4. String? key,
  5. bool isSliver = false,
  6. bool allowNull = false,
  7. X? initialValue,
})

Implementation

Widget builder(
  KType recordId, {
  RecordDataServiceWidgetBuilderWithContext<X?, KType>? builder,
  WidgetDataBuilder<X>? successFn,
  String? key,
  bool isSliver = false,
  bool allowNull = false,

  /// This is useful when the record you want hasn't been loaded by this data
  /// service, but was loaded embedded into another object.  You can use that
  /// local version while you fetch the real one
  X? initialValue,
}) {
  final service = this;
  assert(allowNull == true || (recordId != null || initialValue != null));
  if (recordId == null && initialValue == null) {
    return Builder(builder: (context) => builder!(context, null, this));
  }

  var isInitialized = service.isInitialized(recordId);

  /// If the record is loaded into memory, use that memory value to have an immediate rendering.
  final _initialValue =
      isInitialized ? service.tryGet(recordId) : initialValue;

  if (sunny.get<IAuthState>().isNotLoggedIn) {
    return isSliver ? emptyBox.sliverBox() : emptyBox;
  }
  return StreamBuilder<X>(
    key: Key("${X}${key ?? recordId}"),
    stream: service

        /// We only stream updates because we will have fetched the _initialValue above
        .recordStream(recordId, immediate: _initialValue == null)!
        .where((event) => event != null)
        .cast(),
    initialData: _initialValue,
    builder: (context, snapshot) => allowNull
        ? snapshot.render(
            context,
            isSliver: isSliver,
            builder: (X? data, loading) {
              return data == null
                  ? loading()
                  : builder!(context, data, service);
            },
          )
        : snapshot.render(
            context,
            isSliver: isSliver,
            allowNull: allowNull,
            successFn: successFn ??
                ((X data) {
                  return builder!(context, data, service);
                }),
          ),
  );
}