Unified persistence primitives for Dart and Flutter applications.
unified_persistence provides a small abstraction around:
- a
PersistorAdapterthat reads and writes bytes - a
PersistorSerializer<T>that converts typed values to and from bytes - a
Persistor<T>that combines the two
dependencies:
unified_persistence: ^0.0.3Use Persistor<T> when you want a typed API for saving and restoring values.
import 'package:unified_persistence/unified_persistence.dart';
class UserProfile {
UserProfile({required this.name});
final String name;
Map<String, dynamic> toJson() => {'name': name};
static UserProfile fromJson(Map<String, dynamic> json) {
return UserProfile(name: json['name'] as String);
}
}
final persistor = MemoryPersistor<UserProfile>(
serializer: JsonSerializer<UserProfile>(
toJson: (value) => value.toJson(),
fromJson: UserProfile.fromJson,
),
);
await persistor.persist(UserProfile(name: 'Alice'));
final profile = await persistor.restore();Useful for tests or temporary in-memory caching.
final persistor = MemoryPersistor<Map<String, dynamic>>(
serializer: JsonSerializer<Map<String, dynamic>>(
toJson: (value) => value,
fromJson: (json) => json,
),
);Use FilePersistor<T> when you want to store serialized bytes in a file.
final persistor = FilePersistor<UserProfile>(
filePath: './profile.json',
serializer: JsonSerializer<UserProfile>(
toJson: (value) => value.toJson(),
fromJson: UserProfile.fromJson,
),
);If you only need JSON file persistence, JsonFilePersistor<T> is shorter:
final persistor = JsonFilePersistor<UserProfile>(
filePath: './profile.json',
toJson: (value) => value.toJson(),
fromJson: UserProfile.fromJson,
);Use LocalStoragePersistor<T> or SessionStoragePersistor<T> on web.
final persistor = LocalStoragePersistor<UserProfile>(
key: 'profile',
serializer: JsonSerializer<UserProfile>(
toJson: (value) => value.toJson(),
fromJson: UserProfile.fromJson,
),
);Every persistor also supports synchronous access:
persistor.persistSync(UserProfile(name: 'Alice'));
final profile = persistor.restoreSync();MemoryPersistorworks everywhere.FilePersistorandJsonFilePersistorare fordart:ioplatforms and throw on web.LocalStoragePersistorandSessionStoragePersistorare for web and throw on non-web platforms.
Publishing to pub.dev is automated through GitHub Actions on version tags that
match v{{version}}, for example v0.0.1.
If you create a GitHub Release with a new tag such as v0.0.1, GitHub pushes
that tag and the publish workflow will run automatically.
Before this works, enable automated publishing for this package on pub.dev and configure:
- Repository:
leanflutter/unified_persistence - Tag pattern:
v{{version}}
Notes:
- The version in
pubspec.yamlmust match the release tag. - The first version of a package must still be published manually on pub.dev before automated publishing can be used.