flutter_app_preferences 0.1.0 flutter_app_preferences: ^0.1.0 copied to clipboard
A simple package to manage app preferences and get notifications when something changes.
flutter_app_preferences #
flutter_app_preferences
manages shared preferences in a type-safe way and allows you to receive notifications when one of them changes. Each Preference<T>
extends ValueNotifier<T>
to maximize compatibility with existing solutions and provide seamless experience when using in Flutter.
Status | Comments |
---|---|
Current stable Flutter version | |
Current beta Flutter version | |
The oldest supported Flutter version |
Getting started #
- Add this package to your dependencies.
dependencies:
flutter_app_preferences: latest_version
- Get the dependencies.
flutter pub get
Usage #
- Create a class with your preferences.
// A class that holds the preferences.
class AppPreferences extends BaseAppPreferences {
// An example that stores a boolean value.
final highContrast = Preference('high-contrast', true);
// An example that stores an enum.
final fontSize = Preference(
'font-size',
FontSize.medium,
values: FontSize.values,
);
// An example that stores a custom object.
final currentUser = Preference(
'current-user',
User.initialUser,
fromJson: User.fromJson,
toJson: (user) => user.toJson(),
);
// Provide a list of all the app preferences to ensure that the `AppPreferences` instance can notify its listeners.
@override
List<Preference<Object?>> get props => [
highContrast,
fontSize,
currentUser,
];
}
// Sample enum.
enum FontSize {
small,
medium,
large,
}
// Sample custom object.
class User {
const User({required this.name});
factory User.fromJson(Map<String, Object?> json) =>
User(name: json['name']! as String);
Map<String, Object?> toJson() => {'name': name};
static const initialUser = User(name: '');
final String name;
}
- Initialize an instance of
AppPreferences
.
Future<void> main() async {
final prefs = AppPreferences();
await prefs.initialize();
runApp(const MaterialApp());
}
- Provide it everywhere in your app.
provider
// Provide
runApp(
ChangeNotifierProvider.value(
value: prefs,
child: const MaterialApp(),
),
);
// Read all
final prefs = context.watch<AppPreferences>();
// Read single
final fontSize = context.select<AppPreferences, FontSize>(
(prefs) => prefs.fontSize.value,
);
- Global instance
// Declare
class AppPreferences extends BaseAppPreferences {
static final i = AppPreferences();
}
// Initialize
Future<void> main() async {
await AppPreferences.i.initialize();
runApp(const MaterialApp());
}
// Use - Read
print(AppPreferences.i.highContrast.value);
// Use - Write
AppPreferences.i.highContrast.value = true;
Additional information #
BaseAppPreferences
extendsChangeNotifier
and notifies its listeners when anyPreference
changes.Preference<T>
extendsValueNotifier<T>
and notifies its listeners when its value changes.- You have to call
initialize
on app preferences before accessing anyPreference
. - This package requires at least Flutter 3.24 to work.
- If there are any issues feel free to go to GitHub Issues and report a bug.