get_storage 1.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new90

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:

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:

box.listen((){
  print('box changed');
});

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

box.removeListen(listen);

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');

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 (soon to be 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: #

1- simple Maps storage. 2- cache of http requests 3- storage of simple user information. 4- simple and persistent state storage 5- any situation you currently use sharedPreferences.

When not to use GetStorage: #

1- you need indexes. 2- 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).

Each container has an output for .listenable, so you can use this lib even as a modest persistent state manager using ValueListenableBuilder

[1.2.1] #

  • Remove file dependency

[1.2.0] #

  • Added writeInMemory and save methods

[1.1.6] #

  • Improve io performance

[1.1.5] #

[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

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';

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

class App extends StatelessWidget {
  final box = GetStorage();

  @override
  Widget build(BuildContext context) {
    // We will insert a value into the storage if it does not already have one, otherwise it will ignore it.
    box.writeIfNull('darkmode', false);

    // ValueListenableBuilder is a Flutter widget
    return ValueListenableBuilder(
        valueListenable: box.listenable,
        builder: (c, read, w) {
          bool isDark = box.read('darkmode');
          print(read['darkmode']); // its work too
          return MaterialApp(
            theme: isDark ? ThemeData.dark() : ThemeData.light(),
            home: Scaffold(
              appBar: AppBar(title: Text("Get Storage")),
              body: Center(
                child: SwitchListTile(
                  value: isDark,
                  title: Text("Touch to change ThemeMode"),
                  onChanged: (val) => box.write('darkmode', val),
                ),
              ),
            ),
          );
        });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  get_storage: ^1.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:get_storage/get_storage.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:get_storage/get_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform ios

Because:

  • package:get_storage/get_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform macos

Because:

  • package:get_storage/get_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform web

Because:

  • package:get_storage/get_storage.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package does not support Flutter platform windows

Because:

  • package:get_storage/get_storage.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package not compatible with SDK dart

Because:

  • get_storage that is a package requiring null.

Health suggestions

Format lib/src/storage_impl.dart.

Run flutter format to format lib/src/storage_impl.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
path_provider >=1.6.9 <2.0.0 1.6.11
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
intl 0.16.1
meta 1.1.8 1.2.2
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test