搜索插件、代码仓与 APP 分类

插件详情v2.1.1

get_storage

一个完全用 Dart 编写的快速、超轻量且同步的键值存储

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
21.2万
喜欢数
1880
Pub 分数
130

安装插件

flutter pub add get_storage

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

GetStorage 是一个完全用 Dart 编写的轻量级、同步的键值存储解决方案,旨在跨多个平台实现快速高效的持久化数据存储。它提供了一个简单的 API,用于存储和检索字符串、数字、布尔值和列表等基本数据类型,而无需使用异步操作。非常适合小型应用状态管理或缓存,可在 Android、iOS、Web、macOS、Windows 和 Linux 上无缝运行。其极小的占用空间和易用性使其成为开发者寻求比复杂数据库更简单替代方案的理想选择。

适合场景

  • 存储用户偏好
  • 缓存小数据集
  • 持久化应用设置
  • 管理会话数据
  • 简单的本地状态管理

主要特点

  • 同步操作
  • 轻便且快速
  • 支持多个平台
  • 易于使用的 API
  • 无外部依赖

适合对象

  • 小型到中型应用
  • 快速原型设计
  • 简单数据持久化需求
  • 跨平台开发
  • 应用避免复杂的数据库设置

使用建议

  • 不适用于大型数据集
  • 有限的查询功能
  • 无内置加密
  • 以纯文本形式存储的数据
  • 仅适用于简单的键值对

原始 README

以下为关联源码仓库的 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

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目