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

插件详情v2.0.1

simple_database

一个简单的轻量级包装器,用于 Flutter 的 SharedPreferences 包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
39
喜欢数
6
Pub 分数
140

安装插件

flutter pub add simple_database

插件介绍

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

一个轻量级、易于使用的 Flutter SharedPreferences 包装器,旨在简化 Flutter 应用程序中的持久化数据存储。它通过极少的样板代码抽象了保存、检索和删除键值对等常见操作。非常适合需要基本本地存储但又不想使用完整数据库的小型应用。该包具有跨平台特性,支持 Android、iOS、Web、macOS、Windows 和 Linux,适用于各种部署场景。

适合场景

  • 存储用户偏好
  • 在会话之间保存应用状态
  • 缓存少量数据
  • 管理简单设置
  • 持久化登录令牌
  • 跟踪用户活动

主要特点

  • 最小 API 表面
  • 自动类型序列化
  • 跨平台兼容性
  • 简单设置
  • 轻量级足迹
  • 错误处理

适合对象

  • 初学者Flutter开发者
  • 小型到中型应用
  • 原型和MVP
  • 具有基本持久化需求的应用程序
  • 避免复杂数据库设置的项目

使用建议

  • 不适用于大型数据集
  • 不支持查询或索引
  • 仅限键值存储
  • 数据默认未加密
  • 高频率写入会导致性能下降
  • 最适合用于非关键性、小规模数据

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Simple Database

CI codecov

A simple lightweight wrapper for the SharedPreferences Flutter package. Simple Database provides an abstraction layer for storing objects instead of key, value pairs. This makes it very easy to get a quick local storage solution up and running.

GitHub

Please feel free to contribute

Usage

Please add simple_database as a dependency in your pubspec.yaml file

No Setup Primative Storage

You can easily setup a database of primitive types just by assigning it a name, Simple Database will init the DB if it has not been created yet on the phone for you.

void main() async {
    SimpleDatabase names = SimpleDatabase(name: 'names');

    await names.add('Bob');
    await names.add('Doug');

    for (var name in await names.getAllType<String>()) {
      print(name);
    }
}

Mixed Types

You can use a single database that stores multiple primitive types and then specify what type you want from the database.

void main() async {
    SimpleDatabase names = SimpleDatabase(name: 'names');

    await names.add('Bob');
    await names.add(150)
    await names.add('Doug');
  
    //Print out all strings in the db
    for (var name in await names.getAllType<String>()) {
      print(name);
    }
  
    //Print out at an index, with a type
    var number = await names.getAtType<int>(1);
    print(number);
}

Non-Primitive object storage

You can store your own objects you have created easily just by implementing 2 functions in the class. toJson and fromJson. You will need to pass the fromJson function to the constructor of SimpleDatabase if you want it to rebuild the json into your object. You do not need this, it will just return a hash map with the values from your object if the function is not provided.


class SimpleClass {
  final int age;
  final String name;
  final double height;
  final bool gender;

  SimpleClass(this.age, this.name, this.height, this.gender);

  //Implement fromJson - Required
  SimpleClass.fromJson(Map<String, dynamic> json) : age = json['age'], name = json['name'], height = json['height'], gender = json['gender'];

  //Implement toJson - Required
  Map<String, dynamic> toJson() => {
    'age': age,
    'name' : name,
    'height' : height,
    'gender' : gender,
  };
}

void main() async {
    SimpleDatabase classDB = SimpleDatabase(name: 'class', fromJson: (fromJson) => SimpleClass.fromJson(fromJson));

    SimpleClass john = SimpleClass(18, 'John', 5.2, true);  

    await classDB.add(john);

    SimpleClass person = await classDB.getAtType<SimpleClass>(0);
    print(person.name);
}

TODO:

  • Built in encryption of stored json strings
  • Better documentation

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。