save method

Future<T> save(
  1. T item, {
  2. DataSavePolicy? savePolicy,
  3. Map<String, dynamic>? extra,
  4. Map<String, String>? headers,
  5. bool updateTimestamps = true,
})

Saves an item.

item The item to save. savePolicy Controls whether to save to local storage, remote API, or both. updateTimestamps Whether to automatically update createdAt/updatedAt timestamps. Defaults to true. Set to false if you want to manually control timestamp values.

Implementation

Future<T> save(
  T item, {
  DataSavePolicy? savePolicy,
  Map<String, dynamic>? extra,
  Map<String, String>? headers,
  bool updateTimestamps = true,
}) async {
  // Check if this repository supports server ID negotiation and if the item
  // needs it. We check if 'this' is a RepositoryServerIdMixin

  if (this is RepositoryServerIdMixin<T>) {
    final idMixin = this as RepositoryServerIdMixin<T>;

    // For models that use server-generated IDs, check if this is a new model
    // that needs to be marked as temporary
    if (idMixin.modelUsesServerGeneratedId(item)) {
      final isExisting = await isExistingItem(item);

      if (!isExisting && !idMixin.hasTemporaryId(item)) {
        // This is a new model with a client-generated ID that should be
        // temporary

        idMixin.markAsTemporary(item, item.id);
      }

      if (idMixin.hasTemporaryId(item)) {
        return await _handleServerIdNegotiation(
          item,
          savePolicy: savePolicy,
          extra: extra,
          headers: headers,
          updateTimestamps: updateTimestamps,
        );
      }
    }
  }

  // Standard save flow for client-generated IDs
  return await _handleStandardSave(
    item,
    savePolicy: savePolicy,
    extra: extra,
    headers: headers,
    updateTimestamps: updateTimestamps,
  );
}