single_item_shared_prefs 0.0.4 icon indicating copy to clipboard operation
single_item_shared_prefs: ^0.0.4 copied to clipboard

SharedPreferences/UserDefaults persistent Storage implementation.

single_item_shared_prefs #

SharedPreferences/UserDefaults persistent Storage implementation.

This package is an addon to the single_item_storage package and offers a Storage implementation using the shared_preferences package and dart JSON converters, json.encode and json.decode, to store items.

Getting started #

Create a new instance by providing fromMap and toMap item converters, itemKey as key for this item in shared preferences, and an optional sharedPreferences instance.

Storage<User> storage = CachedStorage<User>(SharedPrefsStorage(
  itemKey: 'model.user.key',
  fromMap: (map) => User.fromMap(map),
  toMap: (item) => item.toMap(),
));

@JsonSerializable()
class User {
  final String id;
  final String email;

  factory User.fromMap(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toMap() => _$UserToJson(this);

  User(this.id, this.email);
}

To store primitive values that don't need a converter use the .primitive named constructor.

/* Supported primitive types: 
 - bool
 - double
 - int
 - String
 - List<String>
 */
SharedPrefsStorage<int>.primitive(itemKey: 'cow_counter')

If the sharedPreferences is omitted, then SharedPreferences.getInstance is used.

Notice that the SharedPrefsStorage is wrapped in CachedStorage to add in-memory caching for better performance.

When defining the to/from map converter mind that the map values can only be: number, boolean, string, null, list or a map with string keys as defined in json.encode and json.decode from the dart:convert package.

This example uses json_serializable as Map converter for convenience.

1
likes
130
pub points
70%
popularity

Publisher

unverified uploader

SharedPreferences/UserDefaults persistent Storage implementation.

Homepage

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter, shared_preferences, single_item_storage

More

Packages that depend on single_item_shared_prefs