Skip to main content

get_storage_wasm 2.1.5 copy "get_storage_wasm: ^2.1.5" to clipboard
get_storage_wasm: ^2.1.5 copied to clipboard

A fast, extra light and synchronous key-value storage written entirely in Dart with WebAssembly support. Based on the original get_storage package.

get_storage_wasm #

Note: This package is based on the original get_storage package by Jonny Borges, with additional features and improvements including WebAssembly support.

A fast, extra light and synchronous key-value in memory, which backs up data to disk at each operation. It is written entirely in Dart and easily integrates with Get framework of Flutter.

Supports Android, iOS, Web, Mac, Linux, and fuchsia and Windows**. Can store String, int, double, Map and List

Note: values are persisted as JSON, so anything you store must be JSON-encodable. A value that is not (e.g. a Color) is dropped and reported in the logs rather than breaking the whole container — see Self-healing flush.

🚀 WebAssembly (WASM) Support #

GetStorage now fully supports WebAssembly compilation for Flutter web apps! This enables:

  • Up to 3x faster execution compared to JavaScript compilation
  • Better memory efficiency and performance
  • Future-proof compatibility with WebAssembly-first Flutter web

Building with WebAssembly: #

# Build your Flutter web app with WebAssembly support
flutter build web --release --wasm

The package automatically uses WebAssembly-compatible storage implementation when compiled with WASM, while maintaining full compatibility with regular JavaScript builds.

Add to your pubspec: #

dependencies:
  get_storage_wasm:

Install it #

You can install packages from the command line:

with Flutter:

$  flutter packages get

About this Package #

This package (get_storage_wasm) is based on the original get_storage package created by Jonny Borges.

Key Improvements: #

  • ✅ WebAssembly (WASM) Support - Full compatibility with Flutter's WASM compilation
  • ✅ Enhanced Performance - Optimized for modern Flutter versions
  • ✅ Updated Dependencies - Compatible with latest Flutter and Dart versions
  • ✅ Improved Documentation - Better examples and usage guides
  • ✅ Self-healing flush - A value that cannot be JSON-encoded is dropped and reported instead of breaking every write in the container
  • ✅ Durable flush - saveSync() / hasPendingFlush let you know exactly what has reached the disk
  • ✅ Structured logging - GetStorage.setLogHandler() routes storage logs into your app's logger

Credits #

Import it #

Now in your Dart code, you can use:

import 'package:get_storage_wasm/get_storage_wasm.dart';

Initialize storage driver with await: #

main() async {
  await GetStorage.init();
  runApp(App());
}

use GetStorage through an instance or use directly GetStorage().read('key')

final box = GetStorage();

To write information you must use write :

box.write('quote', 'GetX is the best');

To read values you use read:

print(box.read('quote'));
// out: GetX is the best

To remove a key, you can use remove:

box.remove('quote');

To listen changes you can use listen:

Function? disposeListen;
disposeListen = box.listen((){
  print('box changed');
});

If you subscribe to events, be sure to dispose them when using:

disposeListen?.call();

To listen changes on key you can use listenKey:

box.listenKey('key', (value){
  print('new key is $value');
});

To erase your container:

box.erase();

If you want to create different containers, simply give it a name. You can listen to specific containers, and also delete them.

GetStorage g = GetStorage('MyStorage');

To initialize specific container:

await GetStorage.init('MyStorage');

Durability #

write(), remove() and erase() update memory immediately and schedule the disk write in the background. The returned future completes when the flush is scheduled, not necessarily when the bytes have reached the disk.

hasPendingFlush #

Whether the container has changes that have not yet reached the disk. Cleared after a successful flush, kept after a failure.

if (box.hasPendingFlush) {
  // Something is not durable yet.
}

saveSync() #

Flushes and awaits the actual file write, so the data survives a force-kill immediately after the await. Unlike save(), which only schedules the flush.

await box.saveSync(); // returns after the bytes are on disk

Use this before advancing a sync watermark (e.g. lastSyncTime), so the watermark can never be newer than what is actually durable locally.

writeInMemory() #

Updates the in-memory value without scheduling a flush. Pair it with saveSync() when you need several writes to land in a single atomic flush:

box.writeInMemory('totalPurchase', 100);
box.writeInMemory('totalConsume', 40);
await box.saveSync(); // one flush for both

Self-healing flush #

Values are persisted as JSON, so a value that json.encode cannot handle (a Color, a DateTime, a Widget, a custom class, …) would normally make every write to that container fail — silently discarding unrelated writes too, until the bad value went away.

Instead, the failing key is identified, reported and removed; the remaining data is flushed normally. Only the bad value is lost, and the container keeps working.

box.write('good', 'kept');
box.write('bad', const Color(0xFF30DDFE)); // not JSON-encodable
await box.saveSync();

box.read('good'); // 'kept' — still persisted
box.read('bad');  // null — dropped and reported in the logs

Logging #

Storage logs can be routed into your app's logger, so they end up in your error reports instead of only in the console. The handler receives every message, including flush failures.

import 'package:get_storage_wasm/get_storage_wasm.dart';

final logger = Logger('GetStorage');
GetStorage.setLogHandler((tag, level, message) {
  switch (level) {
    case StorageLogLevel.error:
      logger.error('[$tag] $message');
      break;
    case StorageLogLevel.warn:
      logger.warn('[$tag] $message');
      break;
    case StorageLogLevel.info:
      logger.info('[$tag] $message');
      break;
    case StorageLogLevel.debug:
      logger.debug('[$tag] $message');
      break;
  }
});

Without a handler, messages are printed to the console as [GetStorage][<container>][<level>] <message>.

SharedPreferences Implementation #

class MyPref {
  static final _otherBox = () => GetStorage('MyPref');

  final username = ''.val('username');
  final age = 0.val('age');
  final price = 1000.val('price', getBox: _otherBox);

  // or
  final username2 = ReadWriteValue('username', '');
  final age2 = ReadWriteValue('age', 0);
  final price2 = ReadWriteValue('price', '', _otherBox);
}

...

void updateAge() {
  final age = 0.val('age');
  // or 
  final age = ReadWriteValue('age', 0, () => box);
  // or 
  final age = Get.find<MyPref>().age;

  age.val = 1; // will save to box
  final realAge = age.val; // will read from box
}

Benchmark Result: #

GetStorage is not fast, it is absurdly fast for being memory-based. All of his operations are instantaneous. A backup of each operation is placed in a Container on the disk. Each container has its own file.

What GetStorage is: #

Persistent key/value storage for Android, iOS, Web, Linux, Mac and Fuchsia and Windows, that combines fast memory access with persistent storage. Optimized for WebAssembly when building Flutter web apps with --wasm flag for enhanced performance.

What GetStorage is NOT: #

A database. Get is super compact to offer you a solution ultra-light, high-speed read/write storage to work synchronously. If you want to store data persistently on disk with immediate memory access, use it, if you want a database, with indexing and specific disk storage tools, there are incredible solutions that are already available, like Hive and Sqflite/Moor.

As soon as you declare "write" the file is immediately written in memory and can now be accessed immediately with box.read(). You can also wait for the callback that it was written to disk using await box.write().

When to use GetStorage: #

  • simple Maps storage.
  • cache of http requests
  • storage of simple user information.
  • simple and persistent state storage
  • any situation you currently use sharedPreferences.

When not to use GetStorage: #

  • you need indexes.
  • when you need to always check if the file was written to the storage disk before starting another operation (storage in memory is done instantly and can be read instantly with box.read(), and the backup to disk is done in the background. To make sure the backup is complete, you can use await, but if you need to call await all the time, it makes no sense you are using memory storage).

You can use this lib even as a modest persistent state manager using Getx SimpleBuilder #

2
likes
140
points
319
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A fast, extra light and synchronous key-value storage written entirely in Dart with WebAssembly support. Based on the original get_storage package.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, get, path_provider

More

Packages that depend on get_storage_wasm