sinew_storage 1.1.0
sinew_storage: ^1.1.0 copied to clipboard
SecureStore, KeyValueStore, processStorageCall and pagedQuery.
sinew_storage #
SecureStore, KeyValueStore, processStorageCall and pagedQuery.
Local storage with the same error model as the network: a corrupted file, a full disk or an unreadable value becomes a typed exception instead of a crash, and a reinstall starts with an empty secure store.
Features #
KeyValueStore: plain preferences, namespaced by name, typed reads that returnnullfor a value of another typeSecureStore: small secrets, encrypted withFieldCipher, with an undecryptable value read as absentprocessStorageCallandstorageHandlers: turn storage failures intoLocalStorageCorruptionException,StorageFullException,StorageReadExceptionandStorageWriteExceptionpagedQuery: a local list that pages withPager.offsetexactly like a remote one, andInspectableStorefor the devtools
Getting started #
flutter pub add sinew_storage
KeyValueStore #
Plain preferences (the theme, onboarding seen), never tokens or personal data:
final prefs = KeyValueStore.create(); // or create(name: 'orders')
await prefs.setString('theme', 'dark');
final theme = await prefs.getString('theme'); // null when missing or of another type
It's SharedPreferencesAsync (no in-memory cache) with every key prefixed <name>., so clear() removes only this
store's keys. In unit tests, set SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty()
(from shared_preferences_platform_interface).
SecureStore #
Small secrets (the refresh token, a data key), never large data:
final secrets = await SecureStore.create(); // or create(namespace: 'session')
await secrets.write('refreshToken', token);
final token = await secrets.read('refreshToken'); // null when absent or undecryptable
await secrets.clearUserData(); // on sign-out
As in Kotlin, the secrets are a KeyValueStore (<namespace>_secure) whose values FieldCipher
(sinew.secure.<namespace>) encrypts, with <namespace>:<key> bound to each value. Only the cipher's key is in
flutter_secure_storage. A value that can't be decrypted (a restore onto another device, tampering) reads as absent and
is deleted. A reinstall starts empty: the values go with the app.
Setup: sinew_security's (CMake to build; on macOS, the Keychain Sharing entitlement). On Android, exclude the
FlutterSecureStorage files and the app's shared preferences from cloud backup and device transfer in
data_extraction_rules.xml: a restored value can't be decrypted on a new device anyway.
In unit tests, use InMemorySharedPreferencesAsync and FlutterSecureStorage.setMockInitialValues({}).
The storage guard #
Guard every store or database call in a repository, as processApiCall guards HTTP:
Future<Result<String?>> refreshToken() =>
processStorageCall('AUTH', 'READ_TOKEN', StorageAccess.read, () => secrets.read('refreshToken'));
| Failure | Becomes |
|---|---|
DecryptionFailed, a FormatException decoding stored data, a plugin's failed decryption (BadPaddingException) |
LocalStorageCorruptionException |
out of space (ENOSPC, "database or disk is full", QuotaExceededError) |
StorageFullException |
any other PlatformException, IOException, SqliteException or browser storage error |
StorageReadException / StorageWriteException |
| an envelope with no payload | ParseException, reported |
| anything else (a bug) | UnexpectedException, reported |
storageHandlers is public: add rows for your database package's own errors and guard with processCall.
Paging a local query #
pagedQuery gives a local table the same PagingEntity a remote list returns, for Pager.offset. It asks your
LIMIT/OFFSET query for one row more than the page, so it knows whether a next page exists without a COUNT(*):
Future<PagingEntity<OrderRow>> page(int page, int limit) => pagedQuery(page, limit, (offset, count) =>
(db.select(db.orders)..orderBy([(o) => OrderingTerm.desc(o.placedAt)])..limit(count, offset: offset)).get());
total and totalPage stay 0. 0-based pages pass firstPage: 0.
InspectableStore #
KeyValueStore and SecureStore are InspectableStores (name, sorted keys(), read(key) as text), which the
devtools browse; implement it on a DAO to show up there too.
Related packages #
Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.
Documentation #
- Usage docs: sinew.srctool.com
- API reference: pub.dev/documentation/sinew_storage
- Source and issues: srctool/sinew-dart
- Design and decisions: the Guide
License #
Apache 2.0, see LICENSE.