OptimisticChange class

Represents a change to be applied optimistically to the local cache.

Optimistic changes allow the UI to update immediately while the server processes the request. When the server responds, the change is either confirmed (kept) or rolled back (reverted).

ID Generation Requirement

Important: Optimistic inserts require client-side ID generation (e.g., UUIDs).

If you use server-generated IDs (e.g., auto-increment), the optimistic row will have a temporary ID that differs from the server-assigned ID. This results in duplicate rows (temp + real) because the SDK cannot automatically map temporary IDs to server-assigned IDs.

// ✅ CORRECT: Client-side UUID
final id = Uuid().v4();
await client.reducers.createNote(
  id: id,
  content: 'Hello',
  optimisticChanges: [OptimisticChange.insert('note', {'id': id, 'content': 'Hello'})],
);

// ❌ PROBLEMATIC: Server-generated ID
// The optimistic row uses a temp ID, but the server assigns a different ID.
// You'll end up with both rows in the cache.

Updates and deletes work with any ID strategy since the row already exists.

Constructors

OptimisticChange.delete(String tableName, Map<String, dynamic> row)
OptimisticChange.fromJson(Map<String, dynamic> json)
factory
OptimisticChange.insert(String tableName, Map<String, dynamic> row)
OptimisticChange.update(String tableName, Map<String, dynamic> oldRow, Map<String, dynamic> newRow)

Properties

hashCode int
The hash code for this object.
no setterinherited
newRowJson Map<String, dynamic>?
final
oldRowJson Map<String, dynamic>?
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tableName String
final
type OptimisticChangeType
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson() Map<String, dynamic>
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

deleteRow<T>(TableCache<T> table, T row) OptimisticChange
Build a delete change from a typed row.
insertRow<T>(TableCache<T> table, T row) OptimisticChange
Build an insert change from a typed row. The SDK extracts the table name and serializes the row via the decoder — no hand-typed map and no stringly-typed table name.
updateRow<T>(TableCache<T> table, T oldRow, T newRow) OptimisticChange
Build an update change from two typed rows (old + new).