搜尋套件、儲存庫與 APP 分類

套件詳情v0.1.0

dbx

一種以簡潔為設計宗旨的資料儲存解決方案。這是一種簡單、輕量且快速的資料庫解決方案,適用於Flutter應用程式。

AndroidiOSmacOSWindowsLinux
近 30 天下載
50
喜歡數
5
Pub 分數
150

安裝套件

flutter pub add dbx

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

dbx 是一個輕量級、快速且簡單的 NoSQL 資料庫解決方案,專為 Flutter 應用程式設計。以簡潔為核心構建,提供跨多個平台的持久化資料儲存的易用介面。適用於需要可靠本機資料處理但又不希望使用傳統資料庫複雜性的應用程式,dbx 支援 Android、iOS、macOS、Windows 和 Linux。其極簡的設定和高效能使其適合中小型資料需求。

適用情境

  • 儲存使用者偏好
  • 快取應用資料
  • 儲存離線使用者內容
  • 管理本地狀態
  • 保留表單數據

主要特色

  • 簡單 API
  • 跨平台支援
  • 輕量化設計
  • 快速讀寫操作
  • 自動數據持久化

適合對象

  • 初學者Flutter開發者
  • 需要本機儲存的應用程式
  • 資料庫需求較少的專案
  • 原型設計和最小可行產品開發
  • 小規模數據管理

使用建議

  • 不適用於大型資料集
  • 有限的查詢功能
  • 無內建同步
  • 最適合單裝置使用
  • 無進階索引功能

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Logo

DBX

dbx is a Dart package designed for secure and efficient local storage using Protocol Buffers instead of JSON.

  • A Key-Value Database with Easy API that supports wide range of Data Types.

Features

  • Protocol Buffers: Utilizes Protocol Buffers (protobuf) for data serialization, offering significant performance improvements over JSON.
  • Secure Storage: Data is securely stored using flutter_secure_storage and encrypted with the encrypt library, with customizable encryption options.
  • Comprehensive Data Support: Supports all data types, including their list variants and JSON objects.
    • What we mean
      • Supported data types are int, double, bool, String, Uint8List, List<int>, List<double>, List<bool>, List<String>, List<Uint8List>, and Map<String, dynamic> (JSON).
      • Uint8List for bytes, JSON for custom objects
  • Efficient and Compact: Protobuf is more efficient and compact compared to JSON, with built-in data compression (gzip), reducing latency and improving app performance.
  • Customizable Encryption: Configure AES modes, key lengths, and IV lengths for enhanced security.
  • Generic Operations: Type-agnostic get/set methods for dynamic data handling.
  • Batch Operations: Efficient bulk read/write operations for multiple keys.
  • Indexing & Search: Fast key lookups, sorted keys, prefix-based searches, and existence checks.
  • Synchronization: Export/import functionality for data backup and cross-device sync.
  • Schema Versioning: Automatic migrations for schema evolution.
  • Utilities: Key count, file size monitoring, and bulk data removal.
  • Familiar Api: Same API as of SharedPreference.
    • Won't miss SharedPreference. No headache, easy adaption.🚀
  • Created With Simplicity in mind
    • No Object Creation, no overhead of state management solution
    • No Object Passing
    • All method are static, just call and use.

Benefits of Using Protocol Buffers Over JSON

  • Performance: Protobuf is significantly faster than JSON for serialization and deserialization.
  • Compactness: Protobuf messages are smaller in size, which reduces storage space and improves transmission speed.
  • Efficiency: Protobuf is more efficient in terms of CPU and memory usage.

Advantages Over Shared Preferences

  • Data Integrity: Protobuf ensures data integrity and consistency, which is crucial for complex data structures.
  • Security: Data is encrypted and securely stored, unlike shared preferences which can be more vulnerable to security breaches.
  • Scalability: Protobuf's efficient serialization makes it suitable for larger datasets and more complex data structures.

Installation

Add dbx to your pubspec.yaml:

dependencies:
  dbx: ^0.1.0

you can install packages from the command line:

with Flutter:

$  flutter pub get

with Dart:

$  dart pub get

Initialization

with Flutter:

import 'package:dbx/dbx.dart';

// Basic initialization
void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await DBX.init();
    runApp(MyApp());
}

// Advanced initialization with custom encryption and path
void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await DBX.init(
      DBXEncryptionConfig(mode: encrypt.AESMode.gcm, keyLength: 32, ivLength: 16),
      '/custom/path/to/storage.dbx' // optional custom file path
    );
    runApp(MyApp());
}

// Sync-enabled initialization (same keySeed across devices)
void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await DBX.init(
      DBXEncryptionConfig(
        mode: encrypt.AESMode.cbc,
        keyLength: 32,
        ivLength: 16,
        keySeed: 'user-specific-shared-secret' // Derive keys from seed
      )
    );
    runApp(MyApp());
}

with dart:

import 'package:dbx/dbx.dart';

// Example usage
void main() async {
  await DBX.init();
}

Usage

Basic Operations
  • Store Data (Typed)
  // Store data
   DBX.setString('message', 'Give it a try!');
   DBX.setString('username', 'john_doe');
   DBX.setInt('age', 30);
   DBX.setDouble('height', 5.9);
   DBX.setBool('isAdmin', true);
   DBX.setBytes(
        'profilePicture', File('assets/profile.jpeg').readAsBytesSync());
   DBX.setStringList('tags', ['dart', 'flutter']);
   DBX.setIntList('scores', [100, 95, 85]);
   DBX.setDoubleList('strike_rate', [36.6, 37.0, 36.8]);
   DBX.setBoolList('attendence', [true, false, true]);
   DBX.setBytesList('files', [
      File('assets/file1.jpeg').readAsBytesSync(),
      File('assets/logo.jpeg').readAsBytesSync(),
      File('assets/file2.jpeg').readAsBytesSync()
    ]);
  • Generic Operations
  // Type-agnostic get/set
  DBX.set('generic_string', 'Hello Generic!'); // String
  DBX.set('generic_int', 42); // int
  DBX.set('generic_list', [1, 2, 3]); // List<int>

  String? value = DBX.get('generic_string');
  int? number = DBX.get('generic_int');
  List<int>? list = DBX.get('generic_list');
  • JSON Objects (for custom Dart objects)
  // Store JSON objects
  DBX.setJson('user_profile', {'name': 'John', 'age': 30, 'active': true});

  // Retrieve JSON objects
  Map<String, dynamic>? profile = DBX.getJson('user_profile');
  // Use with custom objects: User.fromJson(profile!)
Batch Operations
  // Bulk set
  DBX.setStringBulk({'key1': 'value1', 'key2': 'value2'});

  // Bulk get
  Map<String, String?> values = DBX.getStringBulk(['key1', 'key2']);
Indexing & Search
  // Check existence
  bool exists = DBX.containsKey('someKey');

  // Get all keys
  List<String> allKeys = DBX.getAllKeys();

  // Get sorted keys
  List<String> sortedKeys = DBX.getAllKeysSorted();

  // Prefix search
  List<String> appKeys = DBX.getKeysStartingWith('app');
Synchronization & Backup

DBX provides export/import functionality for data backup and synchronization across devices.

Basic Backup and Restore
// Create a backup
Uint8List backupData = await DBX.exportData();

// Save to local file or external storage
await File('/path/to/backup.dbx').writeAsBytes(backupData);

// Restore from backup
Uint8List restoredData = await File('/path/to/backup.dbx').readAsBytes();
await DBX.importData(restoredData);
Cross-Device Synchronization

For syncing data between multiple devices:

  1. Initialize with shared key seed (same on all devices):
await DBX.init(DBXEncryptionConfig(keySeed: 'user-unique-id'));
  1. Sync workflow:
// On source device - export and upload
Uint8List syncData = await DBX.exportData();
await cloudService.uploadSyncData(syncData);

// On target device - download and import
Uint8List downloadedData = await cloudService.downloadSyncData();
await DBX.importData(downloadedData);
Cloud Storage Integration Example
class DBXCloudSync {
  static Future<void> uploadToCloud() async {
    final data = await DBX.exportData();
    // Upload to Firebase Storage, AWS S3, etc.
    await FirebaseStorage.instance
        .ref('user-data/${userId}/dbx-sync')
        .putData(data);
  }

  static Future<void> downloadFromCloud() async {
    final data = await FirebaseStorage.instance
        .ref('user-data/${userId}/dbx-sync')
        .getData();
    if (data != null) {
      await DBX.importData(data);
    }
  }
}

// Usage
await DBXCloudSync.uploadToCloud(); // Backup current data
await DBXCloudSync.downloadFromCloud(); // Restore on new device
Key Points
  • Security: Exported data remains encrypted - safe for cloud storage
  • Atomic: Import replaces entire database state
  • Migration: Automatic schema updates on import
  • Performance: Export/import handles compression internally
  • Conflict Resolution: Last imported data wins (no built-in merging)

Use export/import for periodic backups or when switching devices. For real-time sync, combine with cloud storage webhooks.

Note: For cross-device sync, ensure all devices use the same keySeed during initialization to maintain data compatibility.

Utilities
  // Get statistics
  int keyCount = DBX.keyCount;
  int fileSize = await DBX.fileSize;

  // Remove all data
  DBX.removeAll();

  // Clear and delete file
  DBX.clearAll();
  • Read Data
   @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('DBX Example'),
          centerTitle: true,
        ),
        body: Center(
          child: FutureBuilder(
            future: writeData(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                final files =DBX.getBytesList('files');
                return Column(
                  children: [
                    Image.memory(Uint8List.fromList(
                       DBX.getBytes('profilePicture') ?? [])),
                    Text("Message: ${DBX.getString('message') ?? 'No data'}"),
                    Text(
                        "Username: ${DBX.getString('username') ?? 'No Data'}"), // Output: john_doe
                    Text("Age: ${DBX.getInt('age')}"), // Output: 30
                    Text(
                        "Height: ${DBX.getDouble('height')}"), // Output: 5.9
                    Text(
                        "IsAdmin: ${DBX.getBool('isAdmin')}"), // Output: true
                    Text(
                        "Tags: ${DBX.getStringList('tags') ?? [].join(',')}"), // Output: [dart, flutter]
                    Text(
                        "Scores: ${DBX.getIntList('scores') ?? [].join(',')}"), // Output: [100, 95, 85]
                    Text(
                        "Strike Rate: ${DBX.getDoubleList('strike_rate')}"), // Output: [36.6, 37.0, 36.8]
                    Text(
                        "Attendence: ${DBX.getBoolList('attendence')}"), // Output: [true, false, true]
                    if (files != null)
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          for (final file in files)
                            Image.memory(Uint8List.fromList(file)),
                        ],
                      ),
                  ],
                );
              } else {
                return CircularProgressIndicator();
              }
            },
          ),
        ),
      ),
    );
  }

Contributing

We welcome contributions from the community! If you have any ideas, suggestions, or bug reports, please open an issue or submit a pull request.

Sponsor

If you find this package useful, please consider sponsoring its development to help us continue improving it. You will be mentioned below

Sponsor

License

This project is licensed under the MIT License - see the LICENSE file for details.

TODO

  • Observable Streams: Add reactive streams for real-time data change notifications.
  • Advanced Queries: Implement more complex query operations beyond prefix search.
  • Cloud Integration: Built-in support for popular cloud storage providers.
  • Typed Object Support: Add strongly-typed object serialization with code generation.

所屬分類

依賴

依賴清單 8 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。