UserDefault class
Annotates a field for persistence in standard SharedPreferences.
Best for: UI states, non-sensitive settings, flags, and local caches. Storage: XML file on Android, Plist on iOS.
Complex Scenario: Storing Custom Models
While SharedPreferences only supports primitives natively, this system
automatically handles objects that have toJson and fromJson.
class UserProfile {
final String name;
UserProfile({required this.name});
Map<String, dynamic> toJson() => {'name': name};
static UserProfile fromJson(Map<String, dynamic> json) => UserProfile(name: json['name']);
}
@GeneratePreferences()
class ProfilePrefs extends BasePreferences with _$ProfilePrefs {
@UserDefault('profile_data')
late final UserProfile _profile;
}
Constructors
- UserDefault(String key)
-
const
Properties
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited