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

hashCode → int
The hash code for this object.
no setterinherited
key → String
The unique string key used for storage. Ensure keys are unique across the entire app namespace.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

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