get_storage
2.1.1A fast, extra light and synchronous key-value storage written entirely in Dart
Readme
get_storage
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
Add to your pubspec:
dependencies:
get_storage:
Install it
You can install packages from the command line:
with Flutter:
$ flutter packages get
Import it
Now in your Dart code, you can use:
import 'package:get_storage/get_storage.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');
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.
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
Changelog
[2.1.1]
- Fix null exception in previous getx versions
[2.1.0]
- Add support to Getx5
[2.0.3]
- update to lastest getx
[2.0.2]
- update dependencies
[2.0.0]
- Null-safety support (@Ascenio) and fixes #49, #53 and #54 (@SquishyOctopus)
[1.4.0]
- Added failure recover backup
[1.3.2]
- Added compatibility with Getx 3.15.0
[1.3.1]
- Added compatibility with Getx 3.10.2
[1.3.0]
- Added ReadWriteValue
[1.2.0]
- Added writeInMemory and save methods
[1.1.4]
- Prevent spaces on database.gs
[1.1.3]
- Fix unnecessary imports
[1.1.2]
- Added Benchmarks
[1.1.1]
- Added Examples
[1.1.0]
- Added Container listeners
[1.0.3]
- Added repo
[1.0.2]
- Added docs
[1.0.1]
- Added docs
[1.0.0]
- Release to Get 3.0
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add get_storagepubspec.yaml
name: get_storage
description: A fast, extra light and synchronous key-value storage written entirely in Dart
version: 2.1.1
homepage: https://github.com/jonataslaw/get_storage
environment:
sdk: ">=2.16.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
get: ">=4.0.0 <6.0.0"
path_provider: ^2.0.1
#platform_info: ^3.0.0-nullsafety.1
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 1.0.0 | On demand | Download | e76028d8…c76802 |
| 1.0.1 | On demand | Download | 9b6d0612…0a983e |
| 1.0.2 | On demand | Download | 2a9e7398…b4fabf |
| 1.0.3 | On demand | Download | 3e6b4307…9bf4b2 |
| 1.1.0 | On demand | Download | 4b318825…e0e588 |
| 1.1.1 | On demand | Download | 049ca10e…9bb670 |
| 1.1.2 | On demand | Download | 374a058b…c9e959 |
| 1.1.3 | On demand | Download | 0c345487…131fbf |
| 1.1.4 | On demand | Download | 8c9d188d…816686 |
| 1.1.5 | On demand | Download | 6224cc3a…d059ac |
| 1.1.6 | On demand | Download | bbb75a5b…a8358a |
| 1.2.0 | On demand | Download | 843f6ffc…a99ab1 |
| 1.2.1 | On demand | Download | e7a51b53…218eda |
| 1.3.0 | On demand | Download | 9b3bc431…f63b72 |
| 1.3.1 | On demand | Download | 685e0dd8…91a7c6 |
| 1.3.2 | On demand | Download | 448be644…2b0ea8 |
| 1.4.0 | On demand | Download | e091c2c9…83fe56 |
| 2.0.0 | On demand | Download | 19bde5cf…85b3ba |
| 2.0.1 | On demand | Download | d74d624a…e5e9ac |
| 2.0.2 | On demand | Download | 38f20329…a94081 |
| 2.0.3 | On demand | Download | 00b3908d…a84207 |
| 2.1.0 | On demand | Download | 4af0c47b…5dc998 |
| 2.1.1 | Cached · Upstream SHA-256 matched | Download | 39db1fff…4185a2 |
Dependencies
flutter {
"sdk": "flutter"
}
get "\u003e=4.0.0 \u003c6.0.0"
path_provider "^2.0.1"