local_shared 4.0.0
local_shared: ^4.0.0 copied to clipboard
Lightweight cross-platform secure local storage for Flutter. Native-safe JSON collections and documents with tested CRUD, batch operations, migration semantics, and stream updates.
3.2.0 #
Security Hardening #
- Android Keystore resilience (
resetOnError: true): AddedresetOnError: truetoAndroidOptions. When the Android Keystore key is invalidated (user removes/changes screen lock, app restore across devices, OS update, re-install with different signing key), the corrupted ciphertext is wiped automatically and a fresh key is generated — preventingBadPaddingException/ServiceSpecificExceptioncrashes. - Apple Keychain accessibility (iOS & macOS): Added
IOSOptionsandMacOsOptionswithKeychainAccessibility.first_unlock_this_device. Data is accessible only after the device has been unlocked at least once since boot, preventing reads while the device is fully locked (protects against cold-boot attacks) and keeping access consistent across Apple platforms. - Resilient
_createwith auto-wipe retry: IfFlutterSecureStorage.write()throws (e.g. the Keystore key went away between operations), the entire secure storage is wiped and the write is retried once with a freshly generated key. If the retry also fails,falseis returned gracefully — no crash. - Resilient
_readwith wipe-on-corrupt: IfFlutterSecureStorage.read()throws a decryption exception, corrupted storage is wiped andnullis returned — callers treat this identically to a missing key (clean degradation, no crash). - Corrupted data parsing guard:
decodesafely parses JSON from storage and returnsnullon corrupted or partially written strings rather than allowing an unhandledFormatExceptionto bubble up. - Top-level collection value type checking:
encodegracefully handles top-level Map, List, and primitive entries without throwingTypeError. - Closed stream resilience: All mutation events check
_controller.isClosedbefore emitting, preventingStateErrorif CRUD operations run afterLocalShared.close(). - Atomic migration flag safety:
_migrateAll()only sets the migration completion flag if all keys were migrated without Keystore-level write failures, ensuring any interrupted migration will be safely retried. - Storage wipe stream signal: When an emergency wipe occurs, a sentinel event (
{'__event__': 'storage_wiped'}) is emitted onLocalShared.streamfor app-level handlers. - Safe
initialize()re-invocation: WrappedSharedPreferences.setPrefix()in aStateErrorguard so callinginitialize()multiple times in the same app session (or after another package has already calledSharedPreferences.getInstance()) never throwsBad state: setPrefix cannot be called after getInstance. - Safe non-String
SharedPreferenceshandling in migration: Replacedpreferences.getString(key)with untypedpreferences.get(key)+is! String/is! JSONchecks in_migrateAll()and_read(), preventingTypeErrorstartup crashes when apps storeint,bool,double, orList<String>entries inSharedPreferences. - Encode before Keystore retry in
_create(): JSON validation/encoding now runs before the Keystore retrytryblock, ensuring invalid Dart types (ArgumentError) returnSharedNonecleanly without triggering_wipeStorage()on existing secure data. - Resilient
_deletewith no-op on key-gone: Ifdelete()throws because the key was already wiped (e.g. by a previous Keystore reset), the error is swallowed andtrueis returned — the data is effectively gone, so the operation is considered successful.
Performance Improvements #
- Eliminated redundant post-write secure storage reads:
SharedDocument,SharedCollection, andSharedManyDocumentpreviously re-read and decrypted the entire collection fromFlutterSecureStorageafter every write just to populate stream events and return values. They now reuse the already-updated in-memory collection map (1read +1write instead of3reads +1write). - Fixed N+1 read loop in
SharedManyDocument.create()andupdate(): Replaced the per-IDShared._read()inside the return-data loop with an O(1) in-memory map lookup — batch operations onNdocuments now perform1secure read instead ofN + 2.
CI Fix #
- Bumped GitHub Actions Flutter version from
3.22.3→3.27.4(Dart 3.7). This resolves thepub getfailure caused byshared_preferences >=2.3.4requiring Dart SDK>=3.5.0while the old runner only had Dart 3.4.4.
3.1.1 #
- Change documentation
3.1.0 #
New Features #
- Secure-first storage: All writes go exclusively to
FlutterSecureStorage.SharedPreferencesis no longer a write target. - Automatic bulk migration on
initialize(): On the first call after upgrading, all data inSharedPreferencesis scanned and migrated toFlutterSecureStoragein a single pass. Secure storage wins on key conflicts — existing secure data is never overwritten. - Lazy per-key migration in
_read(): Any key still found inSharedPreferencesduring a read (e.g. after a partial migration) is migrated on-the-spot — written to secure storage and removed fromSharedPreferences— so the next access is fully secure. - Migration guard flag: A
__local_shared_migrated__boolean is stored inSharedPreferencesafter the first bulk pass, so the expensive scan only runs once per installation.
Improvements #
- Delete operations now remove from both
FlutterSecureStorageandSharedPreferences, handling partially-migrated or legacy data gracefully. - Comprehensive migration test suite added (
test/src/shared_migration_test.dart) covering bulk migration, lazy migration, secure-first writes, backward compatibility, conflict resolution, and edge cases.
Breaking Changes #
None. Fully backward-compatible. Existing apps using SharedPreferences as the backing store are seamlessly and automatically upgraded to FlutterSecureStorage on the next initialize() call.
1.0.0 #
- Initial release
1.0.1 #
- Update documentation
1.0.2 #
- Update link
1.0.3 #
- Update README
1.0.4 #
- Update extension syntax
1.0.5 #
- Remove assertion
2.0.0 #
- Add secure storage
2.0.1 #
- Replace all 'SharedPreferences' with 'Flutter Secure Storage'
3.0.0 #
- Some parameters have been replaced by different rules, which will definitely introduce breaking changes
- Full coverage test
4.0.0 #
- AI security test, bug finder, and huge amount of improvements on migrating from the use of SharedPreferences to FlutterSecureStorage
