CloudSyncSharedPreferencesAdapter
A lightweight local implementation of the SyncAdapter
from the cloud_sync
package, powered by SharedPreferences
.
Perfect for simple sync needs, offline caching, prototypes, or fallback storage β no database required.
β¨ Features
- π Persists metadata (
SyncMetadata
) and detail content (String
) inSharedPreferences
. - β‘ Lightweight and fast β ideal for prototyping or minimal sync flows.
- π΄ Supports offline storage with zero setup.
- π Fully compatible with the
cloud_sync
framework.
π¦ Installation
Add to your pubspec.yaml
:
dependencies:
cloud_sync: ^latest
shared_preferences: ^latest
cloud_sync_shared_preferences_adapter: ^latest
π Usage
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:cloud_sync_shared_preferences_adapter/cloud_sync_shared_preferences_adapter.dart';
import 'your_models/my_metadata.dart';
void main() async {
final prefs = await SharedPreferences.getInstance();
final adapter = CloudSyncSharedPreferencesAdapter<MyMetadata>(
preferences: prefs,
metadataToJson: (meta) => jsonEncode(meta.toJson()),
metadataFromJson: (json) => MyMetadata.fromJson(jsonDecode(json)),
getMetadataId: (meta) => meta.id,
isCurrentMetadataBeforeOther: (a, b) => a.updatedAt.isBefore(b.updatedAt),
);
// Use this adapter with CloudSync to persist your data locally.
}
π§ Class Overview
class CloudSyncSharedPreferencesAdapter<M extends SyncMetadata>
extends SerializableSyncAdapter<M, String>
π οΈ Constructor Parameters
Parameter | Description | Required | Default |
---|---|---|---|
preferences |
Instance of SharedPreferences . |
β | β |
metadataToJson |
Serializes metadata to a String . |
β | β |
metadataFromJson |
Deserializes a String into metadata. |
β | β |
getMetadataId |
Extracts the unique ID from a metadata object. | β | β |
isCurrentMetadataBeforeOther |
Compares two metadata objects for version ordering. | β | β |
prefix |
Optional prefix for namespacing stored keys. | β | "$CloudSyncSharedPreferencesAdapter" |
β When to Use
- β‘ Quick local sync for small apps or offline features.
- π§ͺ Ideal for demos, prototypes, or testing sync logic.
- π Acts as a fallback when a cloud adapter is unavailable.
β οΈ Limitations
- Not suitable for large or binary data β use file or database-based adapters instead.
- SharedPreferences has platform-specific size limits (~1β2MB).
π Related Packages
cloud_sync
β Core sync logic.shared_preferences
β Simple local key-value store.
π License
MIT (or match your projectβs license).
Libraries
- Support for doing something awesome.