ConflictResolver class

Handles data conflicts during synchronization.

When the same document is modified on multiple devices between syncs, a conflict occurs. The ConflictResolver uses the configured strategy to determine which version should be kept.

Example:

final resolver = ConflictResolver(
  strategy: ConflictStrategy.lastWriteWins,
);

final winner = resolver.resolve(
  localData: {'text': 'Local version', 'completed': true},
  remoteData: {'text': 'Remote version', 'completed': false},
  localTimestamp: DateTime(2024, 1, 1, 10, 0),
  remoteTimestamp: DateTime(2024, 1, 1, 10, 5),
);

// winner will be remoteData because it's more recent

Custom conflict resolution example:

final resolver = ConflictResolver(
  strategy: ConflictStrategy.custom,
  customResolver: (local, remote, localTime, remoteTime) {
    // Merge arrays instead of replacing
    return {
      ...remote,
      'tags': [
        ...List<String>.from(local['tags'] ?? []),
        ...List<String>.from(remote['tags'] ?? []),
      ].toSet().toList(),
    };
  },
);

Constructors

ConflictResolver({ConflictStrategy strategy = ConflictStrategy.lastWriteWins, CustomConflictResolverCallback? customResolver})

Properties

customResolver CustomConflictResolverCallback?
Custom conflict resolver function (required when strategy is custom).
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
strategy ConflictStrategy
The strategy to use for resolving conflicts.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resolve({required Map<String, dynamic> localData, required Map<String, dynamic> remoteData, required DateTime localTimestamp, required DateTime remoteTimestamp}) Map<String, dynamic>
Resolves a conflict between local and remote versions of a document.
toString() String
A string representation of this object.
inherited

Operators

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