shared_preferences_dtx 1.0.0 shared_preferences_dtx: ^1.0.0 copied to clipboard
A package that simplifies the access most of used properties/functions right in the package shared_preferences.
shared_preferences_dtx #
This extension package provides easy access to the most commonly used properties, functions, and additional convenience methods that depend on the package shared_preferences.
Install #
Add it in your pubspec.yaml
:
dependencies:
shared_preferences_dtx:
Import it where you want to use it.
import 'package:shared_preferences_dtx/shared_preferences_dtx.dart';
Extensions #
final preferences = await SharedPreferences.getInstance();
// set value
await preferences.setValue('String', 'String'),
await preferences.setValue('bool', false),
await preferences.setValue('int', 2),
await preferences.setValue('double', 2.5),
await preferences.setValue('list', <String>['foo', 'bar']),
// get value
preferences.getValue<String>('String');
preferences.getValue<bool>('bool');
preferences.getValue<int>('int');
preferences.getValue<double>('double');
preferences.getValue<List<String>>('list');
// get value or else
preferences.getOrElse('String', defaultValue: 'value');
preferences.getOrElse('bool', defaultValue: false);
preferences.getOrElse('int', defaultValue: 0);
preferences.getOrElse('double', defaultValue: 3.9);
preferences.getOrElse('list', defaultValue: <String>[]);
Before:
final preferences = await SharedPreferences.getInstance();
// set value
await preferences.setString('String', 'String'),
await preferences.setBool('bool', false),
await preferences.setInt('int', 2),
await preferences.setDouble('double', 2.5),
await preferences.setStringList('list', <String>['foo', 'bar']),
// get value
preferences.getString('String') ?? 'value';
preferences.getBool('bool') ?? false;
preferences.getInt('int') ?? 0;
preferences.getDouble('double') ?? 3.9;
preferences.getStringList('list') ?? <String>[];
New Preferences #
// set value
await preferences.setValue('color', Colors.black);
await preferences.setValue('theme_mode', ThemeMode.dark);
await preferences.setValue('user', {'name': 'user', 'age': 20});
// get value
preferences.getValue<Color>('color');
preferences.getValue<ThemeMode>('theme_mode');
preferences.getValue<Map<String, dynamic>>('user');
// get value or else
preferences.getOrElse('color', defaultValue: Colors.black);
preferences.getOrElse('theme_mode', defaultValue: ThemeMode.dark);
preferences.getOrElse('user', defaultValue: {'name': 'user', 'age': 20});
or
// set value
await preferences.setColor('color', Colors.black);
await preferences.setThemeMode('theme_mode', ThemeMode.dark);
await preferences.setJson('user', {'name': 'user', 'age': 20});
// get value
preferences.getColor('color') ?? Colors.black;
preferences.getThemeMode('theme_mode') ?? ThemeMode.light;
preferences.getJson('user') ?? {'name': 'user', 'age': 20};