save method

Future<T?> save()

Persist dirty fields, replacing optimistic data with server confirmation.

Implementation

Future<T?> save() async {
  if (!state.isDirty) return null;
  final before = state;
  applyOptimistic();
  state = state.copyWith(isSaving: true, clearError: true);
  final graph = ref.read(entityGraphProvider);
  try {
    final transport = ref.read(entityTransportRegistryProvider).get<T>(type);
    final row = await transport.update(id, before.edits);
    _removeOptimisticPatch();
    graph.upsertEntity(
      type,
      id,
      entityToGraphRow(row, toGraph ?? transport.toGraph),
    );
    graph.markEntitySynced(type, id);
    graph.invalidateListsForType(type);
    state = EditBuffer(original: graph.readEntity(type, id) ?? const {});
    return row;
  } on Object catch (error) {
    final typed = toEntityGraphError(error);
    _restorePreviousPatch();
    state = before.copyWith(isSaving: false, error: typed);
    throw typed;
  }
}