shared_prefs_typed 1.0.0
shared_prefs_typed: ^1.0.0 copied to clipboard
Code generator for creating type-safe, boilerplate-free SharedPreferences classes in Dart & Flutter.
Changelog #
All notable changes to this project will be documented in this file.
1.0.0 #
Breaking #
- Generated files are now
partfiles — addpart 'filename.g.dart';to your schema file (below your imports). Without this directive the build will fail. See the README Quick Start for the updated template. onReadErroris now a constructor parameter — pass it at construction time:ClassName(backend, onReadError: cb)orClassName.init(onReadError: cb).@TypedPrefs(async: true)removed — use@TypedPrefs(mode: PrefsMode.async).XxxImplBaserenamed toXxxBase— update DI registrations, type annotations, and mocks.isSetFoo()renamed tocontainsFoo().constremoved from generated constructorresetInstance()annotated@visibleForTesting.
New #
List<bool>support — stored asList<String>("true"/"false").List<Enum>support — stored asList<String>via.name; read back via.values.byName(with crash guard).- Non-nullable
DateTimegetters — via@PrefDateTime(encoding, defaultMillis: N). clearAll()— removes all keys owned by the class.onReadErrorcallback — pass to constructor orinit()to forward read errors to a crash reporter.dart:developerlog on read failure — emitted under the'shared_prefs_typed'name; no-op in release builds.
Fixed #
- Enum getters no longer crash on stale stored values —
values.byNameis now wrapped in try/catch; returns the default instead of throwingArgumentError. List<int>/List<double>getters no longer crash on corrupted elements —parseis now wrapped in try/catch; returns the default list instead of throwingFormatException.clearAll()docstring — explicitly notes the operation is not atomic.
0.7.0 #
Re-run
flutter pub run build_runner build.
New #
- Reserved name validation — generator throws a build-time error when a field name collides with a built-in generated member (
init,instance,resetInstance). Use@PrefKeyto assign a different storage key. - Non-nullable getter for nullable fields with non-null default —
static const int? retryCount = 3generatesint get retryCount(non-nullable getter, nullable setter). Eliminates!at call sites. - Public schema classes — annotated class no longer needs a leading
_._AppPrefs→AppPrefs;AppPrefs→AppPrefsImpl. - Immutable list getters — all
List<T>getters returnUnmodifiableListView.dart:collectionis only imported when list fields are present. List<int>/List<double>support — serialized viatoString()/int.parse/double.parse. Nullable variants and typed default literals supported.@PrefKey('key')— overrides the storage key per field. Generator rejects empty and duplicate keys.- Enum support — stored as
.name, parsed via.values.byName(). DateTimesupport (nullable only) — requires@PrefDateTime(DateTimeEncoding.millisecondsSinceEpoch)or.iso8601.- Public
constconstructor —ClassName(prefs)for DI/testing. - Concurrency-safe
init()— memoizes the future; safe to call multiple times. resetInstance()— clears both_instanceand_initFuturefor test teardown.
Removed #
create()andfromBackend()— useinit()orClassName(prefs).
Fixed #
- Type-migration crash — primitive getters (
int,double,bool,String,DateTimemillis) are wrapped intry/catch; aTypeErroron stale storage data returns the field default instead of crashing. - Data-loss warning — generated file includes a header comment noting that renaming a field changes its storage key unless
@PrefKeypins it. DateTime.parsewrapped intry-catch; returnsnullon malformed data.analyzer10.0.1 compatibility — null-checkconstantValue.typebefore use.
Docs #
- Added "Renaming Fields & Data Migration" and "Out of Scope" sections to the README.
0.6.1 #
- Fixed README.md
0.6.0 #
Breaking change — re-run flutter pub run build_runner build after upgrading. No user code changes required unless you accessed _prefs directly (which was private and unsupported).
New #
- Generated class now has a public constructor that accepts the storage backend directly (
AppPreferences(prefs)). Enables constructor injection for DI frameworks (GetIt, Riverpod, provider) and clean per-test instances. static AppPreferences? _instancereplaces the old eagerstatic final instance. Theinstancegetter now throws a descriptiveStateErrorbeforeinit()instead of aLateInitializationError.resetInstance()static method resets_instancetonullfor test teardown.generateInterface: trueannotation option generates an abstractAppPreferencesBaseclass. Enables typed DI registrations and Mockito/Mocktail mocks without touching the concrete class.- Async-mode
init()is now synchronous internally (noawait) while preserving itsFuture<void>return type.
Migration #
After upgrading, run:
flutter pub run build_runner build
The init() / instance API is fully backward compatible.
Fixed #
- Fix: Removed redundant
?? nullfrom generated nullable getters - Fix: Removed unnecessary
as TypeNamecasts in generated nullable setters - Fix: Unsupported field types now produce a clear build-time error instead of uncompilable code
- Fix: Improved string escaping for default values (handles
\,$, newlines) - Fix: Removed redundant
return;from generated asyncinit()method - Removed unused dependencies (
meta,build_config) - Added async mode and error case generator tests
0.5.2 #
- Fixed supported platforms
0.5.1 #
- Fixed builder config
0.5.0 #
2025-07-25
Initial Release #
This is the first release of shared_prefs_typed.
- Type-Safe Code Generation: Automatically generates a singleton service class for accessing
SharedPreferencesbased on a simple, abstract schema class. - Dual Access Modes:
- Synchronous Getters (Default): Provides fast, synchronous getters for performance-critical access (e.g., UI code).
- Asynchronous Getters (
async: true): ProvidesFuture-based getters for cases where data may be updated externally.
- Full Type Support: Supports all types allowed by
shared_preferences:int,double,bool,String,List<String>, and their nullable variants. - Boilerplate Reduction: Eliminates the need for manual key management and repetitive
get/setmethods. - Testable by Design: Integrates seamlessly with
shared_preferences_platform_interfacefor easy and reliable testing.
