Skip to content

hyper-designed/hyper_storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hyper_storage


Quickstart • Documentation • Example

Hyper Storageunifies local key-value storage in Flutter with a single, consistent API. Switch between shared_preferences, hive_ce, flutter_secure_storage or any custom backend without changing a single line of app logic.


Features

  • âś… Simple API: Easy to use API for storing and retrieving data.
  • âś… Multiple Backends: Supports different backends like InMemory, hive, shared_preferences, and flutter_secure_storage (via separate packages).
  • âś… Typed Storage: Store and retrieve data with type safety.
  • âś… JSON Serialization: Store and retrieve custom objects by providing toJson and fromJson functions.
  • âś… Named Containers: Organize your data into named containers for better structure.
  • âś… Custom Serializable Objects Support: Easily store and retrieve custom objects by providing serialization functions.
  • âś… Reactivity: Listen to changes in the storage and react accordingly.
  • âś… Asynchronous: All operations are asynchronous, making it suitable for Flutter applications.
  • âś… Cross-Platform: Works on mobile, web, and desktop platforms.
  • âś… Fully Tested: Comprehensive test coverage to ensure reliability.
  • âś… Mockable: Easy to mock for testing purposes.

Getting started

Add hyper_storage to your pubspec.yaml dependencies:

dependencies:
  hyper_storage: ^0.1.0 # Replace with the latest version

  hyper_storage_flutter: ^0.1.0 # Add this if you're using Flutter.

  # Add one of the available backends of your choice:
  hyper_storage_hive: ^0.1.0
  hyper_storage_shared_preferences: ^0.1.0
  hyper_storage_secure: ^0.1.0

Then, run flutter pub get or dart pub get.

Usage

Initialize the storage with the backend you want to use.

import 'package:hyper_storage/hyper_storage.dart';
import 'package:hyper_storage_shared_preferences/shared_preferences_backend.dart';

void main() async {
  // Initialize the storage with SharedPreferences
  final storage = await HyperStorage.init(backend: SharedPreferencesBackend());

  // Set a value
  await storage.setString('name', 'Hyper Storage');

  // Get a value
  final name = await storage.getString('name');
  print(name); // Output: Hyper Storage

  // Close the storage
  await storage.close();
}

For more detailed examples, please see the example.md file.

Available Backends

  • InMemoryBackend: A simple in-memory backend for temporary storage.

More backends are available in separate packages:

Contents

Initialization

You can initialize hyper_storage with any storage backend that implements the StorageBackend interface. Always pass the backend you want to use to HyperStorage.init.

In-memory (for tests or ephemeral data)

import 'package:hyper_storage/hyper_storage.dart';

final storage = await HyperStorage.init(backend: InMemoryBackend());

Using other backends

Add the desired backend package to your dependencies and provide the backend instance to init:

import 'package:hyper_storage/hyper_storage.dart';
import 'package:hyper_storage_hive/hyper_storage_hive.dart';

final storage = await HyperStorage.init(backend: HiveBackend());

Basic Operations

You can use the set and get methods to store and retrieve data.

// Set a value
await storage.set('name', 'Hyper Storage');

// Get a value
final name = await storage.get('name');
print(name); // Output: Hyper Storage

Typed Data

hyper_storage provides typed methods to store and retrieve data with type safety.

Storing Data

await storage.setString('name', 'John');
await storage.setInt('age', 30);
await storage.setBool('isDeveloper', true);
await storage.setDouble('height', 1.75);
await storage.setStringList('skills', ['Dart', 'Flutter', 'JavaScript']);
await storage.setBytes('data', Uint8List.fromList([1, 2, 3, 4, 5]));

Getting Data

final String? name = await storage.getString('name');
final int? age = await storage.getInt('age');
final bool? isDeveloper = await storage.getBool('isDeveloper');
final double? height = await storage.getDouble('height');
final List<String>? skills = await storage.getStringList('skills');
final Uint8List? data = await storage.getBytes('data');

JSON Data

You can store and retrieve JSON data (Map<String, dynamic>) and lists of JSON data (List<Map<String, dynamic>>).

Storing JSON

await storage.setJson('profile', {
    'name': 'John Doe',
    'age': 30,
    'isDeveloper': true,
    'height': 1.75,
    'skills': ['Dart', 'Flutter', 'JavaScript'],
});

await storage.setJsonList('items', [
    {'id': 1, 'name': 'Item 1'},
    {'id': 2, 'name': 'Item 2'},
    {'id': 3, 'name': 'Item 3'},
]);

Getting JSON

final Map<String, dynamic>? profile = await storage.getJson('profile');
final List<Map<String, dynamic>>? items = await storage.getJsonList('items');

Named Containers

You can use named containers to organize your data.

final container = await storage.container('account');
await container.setString('username', 'john_doe');
final String? username = await container.getString('username');

Object Containers

You can store and retrieve custom objects by providing toJson and fromJson functions.

class User {
  final String id;
  final String name;

  User({required this.id, required this.name});

  factory User.fromJson(Map<String, dynamic> json) => User(
        id: json['id'] as String,
        name: json['name'] as String,
      );

  Map<String, dynamic> toJson() => {'id': id, 'name': name};
}

final users = await storage.jsonContainer<User>(
  'users',
  fromJson: User.fromJson,
  toJson: (user) => user.toJson(),
  idGetter: (user) => user.id,
);

await users.add(User(id: 'user1', name: 'John'));
final User? user = await users.get('user1');
final List<User> allUsers = await users.getValues();

Checking for Keys

You can check if a key exists in the storage.

final bool containsName = await storage.containsKey('name');

Closing the Storage

It's important to close the storage when it's no longer needed to release resources.

await storage.close(); // closes all containers.

Mocking for Tests

For testing, you can use a mocked version of the storage.

final storage = await HyperStorage.initMocked();

You can also provide initial data to the mocked storage.

final storage = await HyperStorage.initMocked(initialData: {
  'name': 'Test',
  'age': 25,
});

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request.

License

BSD 3-Clause License

Copyright (c) 2025, Hyperdesigned

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

A unified, backend-agnostic key-value storage layer for Flutter.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 2

  •  
  •  

Languages