mem_value 0.2.2 copy "mem_value: ^0.2.2" to clipboard
mem_value: ^0.2.2 copied to clipboard

Type-safe persistent storage wrapper with automatic serialization for Flutter.

Introduction #

MemValue simplifies syntax for persistent or stored values like SharedPreferences (or similar packages). This addition provides type safety to ensure loading and storing values of the same type. Meaning, when you define a MemValue of type int, only an int value will be stored. This is useful for ensuring that the value you are retrieving is the type you expect.

Important: You need to provide MemValue with a storage method before using any MemValue.

Example App #

Check out the example app for a complete demonstration of all MemValue features with an interactive Flutter UI.

Define your MemValue like so: #

var username = MemString('uniqueTag');

Load your value like so: #

await username.load();

Then, write and read like so: #

username.value = "Hello World";
print(username.value);

Assigning a value to username.value will automatically save the value to storage. To wait for the async operation, use await username.setValue().

Features #

  • Easy to use syntax.
  • Use your perfered storage method.
  • 20 Defined types (including custom).
  • MemChoices wrapper to ensues only a set of values are storred.
  • MemGroup to load/reset multiple values.

Getting started #

1. Define your MemStorage class: #


class MyMemStorage extends MemStorage {
  Future<String?> read(String tag)async{
    // Your code here
  }
  Future<void> write(String tag, String value)async{
    // Your code here
  }
  Future<void> delete(String tag)async{
    // Your code here
  }
}

Alternatively declare a MemStorageDelegate: #

var memStorageDelegate = MemStorageDelegate(
  read: (tag) async {
    // Your code here
  },
  write: (tag, value) async {
    // Your code here
  },
  delete: (tag) async {
    // Your code here
  },
);

2. Setup Storage: #

In main, call MemValue.setup to define storage

void main() async {
    /* .. .*/

    // Setting up MemStorage
    MemValue.setStorage(MyMemStorage());

    // --OR--

    // Setting up MemStorage using delegate
    MemValue.setStorage(memStorageDelegate);

    /* ... */

    runApp(MyApp());
}

3. Define MemValue: #

var username = MemString('uniqueTag');

4. Load MemValue: #

await username.load();

Note: you may also define MemGroup to load multiple values like so:

var memValue1 = MemString('uniqueTag1');
var memValue2 = MemString('uniqueTag2');

var memGroup = MemGroup([
    memValue1,
    memValue2,
]);

await memGroup.loadAll();

Install package: #

flutter pub add mem_value

Usage #

Declare a MemValue with init value #

var username = MemString('uniqueTag', initValue: "Hello");

Reset MemValue #

Resets value to it's inital value.

await username.reset();

Declare a presistant MemValue #

Presistant flag avoid resetting value when reset is called. Useful for storing values that should never be reset to inital value.

var username = MemString('uniqueTag', ignoreReset: true);

Define MemValues #

Non-nullable types:

  • memValue
  • MemDouble
  • MemString
  • MemBool
  • MemList
  • MemSet
  • MemMap
  • MemObject
  • MemCustom
  • MemChoices

Nullable types:

  • NmemValue
  • NMemDouble
  • NMemString
  • NMemBool
  • NMemList
  • NMemSet
  • NMemMap
  • NMemObject
  • NMemCustom
  • NMemChoices

Finally #

Hope you find this package useful. I am open for suggestions and contributions. Please feel free to open an issue or pull request.

0
likes
160
points
24
downloads

Publisher

unverified uploader

Weekly Downloads

Type-safe persistent storage wrapper with automatic serialization for Flutter.

Repository
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on mem_value