shared_preferences_dtx

Last updated:

0 purchases

shared_preferences_dtx Image
shared_preferences_dtx Images
Add to Cart

Description:

shared preferences dtx

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:
shared_preferences_dtx:
copied to clipboard
Import it where you want to use it.
import 'package:shared_preferences_dtx/shared_preferences_dtx.dart';
copied to clipboard
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>[]);
copied to clipboard
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>[];
copied to clipboard
Default Value #
You can use the getTypeOrElse instead of getType ?? defaultValue, for the any types.
preferences.getString('String') ?? 'value';
preferences.getBool('bool') ?? false;
copied to clipboard
Or with extensions:
preferences.getStringOrElse('String', defaultValue: 'value') ;
preferences.getBoolOrElse('bool', defaultValue: false) ;
copied to clipboard
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});
await preferences.setValue('date_time', DateTime(2022, 07, 12)));

// get value
preferences.getValue<Color>('color');
preferences.getValue<ThemeMode>('theme_mode');
preferences.getValue<Map<String, dynamic>>('user');
preferences.getValue<DateTime>('date_time');

// 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});
preferences.getOrElse('date_time', defaultValue: DateTime(2022, 07, 12));
copied to clipboard
or
// set value
await preferences.setColor('color', Colors.black);
await preferences.setThemeMode('theme_mode', ThemeMode.dark);
await preferences.setJson('user', {'name': 'user', 'age': 20});
await preferences.setDateTime('date_time', DateTime(2022, 07, 12)); // saves in utc

// get value
preferences.getColor('color') ?? Colors.black;
preferences.getThemeMode('theme_mode') ?? ThemeMode.light;
preferences.getJson('user') ?? {'name': 'user', 'age': 20};
preferences.getDateTime('date_time') ?? DateTime(2022, 07, 12); // utc
copied to clipboard

Date time is saved as timezone independent milliseconds from the standard Unix epoch.

📝 License #
Copyright © 2021 Zambiee.
This project is MIT licensed.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.