搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。