build method

  1. @override
Future<EntitySnapshot<T>> build({
  1. required String type,
  2. required String? id,
  3. required EntityDecoder<T> fromGraph,
  4. EntityEncoder<T>? toGraph,
  5. bool enabled = true,
  6. bool subscribe = true,
})

Implementation

@override
Future<EntitySnapshot<T>> build({
  required String type,
  required String? id,
  required EntityDecoder<T> fromGraph,
  EntityEncoder<T>? toGraph,
  bool enabled = true,
  bool subscribe = true,
}) async {
  _disposed = false;
  _ready = false;
  final graph = ref.watch(entityGraphProvider);
  _viewRegistration?.dispose();
  final entityId = id;
  _viewRegistration = entityId == null
      ? null
      : graph.registerView(
          GraphViewDefinition(
            viewId: 'entity:${type.length}:$type:$entityId',
            label: '$type detail',
            kind: GraphViewKind.entity,
            entityType: type,
          ),
          membership: [GraphEntityIdentity(type: type, id: entityId)],
        );
  if (subscribe) {
    ref.watch(entityChangeBridgeProvider<T>(type: type));
  }

  await _graphSubscription?.cancel();
  _graphSubscription = graph.changes.listen(_onGraphChange);
  ref.onDispose(() {
    _disposed = true;
    _viewRegistration?.dispose();
    unawaited(_graphSubscription?.cancel());
  });

  if (!enabled || id == null) {
    _ready = true;
    return _buildSnapshot(graph);
  }
  await _doFetch(graph);
  _ready = true;
  return _buildSnapshot(graph);
}