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.
- âś… Simple API: Easy to use API for storing and retrieving data.
- âś… Multiple Backends: Supports different backends like
InMemory
,hive
,shared_preferences
, andflutter_secure_storage
(via separate packages). - âś… Typed Storage: Store and retrieve data with type safety.
- âś… JSON Serialization: Store and retrieve custom objects by providing
toJson
andfromJson
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.
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
.
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.
InMemoryBackend
: A simple in-memory backend for temporary storage.
More backends are available in separate packages:
- hyper_storage_hive: Hive backend for persistent storage.
- hyper_storage_shared_preferences: SharedPreferences backend for persistent storage.
- hyper_storage_secure: Secure storage backend for sensitive data.
- Initialization
- Basic Operations
- Typed Data
- JSON Data
- Named Containers
- Object Containers
- Checking for Keys
- Closing the Storage
- Mocking for Tests
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
.
import 'package:hyper_storage/hyper_storage.dart';
final storage = await HyperStorage.init(backend: InMemoryBackend());
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());
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
hyper_storage
provides typed methods to store and retrieve data with type safety.
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]));
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');
You can store and retrieve JSON data (Map<String, dynamic>
) and lists of JSON data (List<Map<String, dynamic>>
).
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'},
]);
final Map<String, dynamic>? profile = await storage.getJson('profile');
final List<Map<String, dynamic>>? items = await storage.getJsonList('items');
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');
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();
You can check if a key exists in the storage.
final bool containsName = await storage.containsKey('name');
It's important to close the storage when it's no longer needed to release resources.
await storage.close(); // closes all containers.
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,
});
Contributions are welcome! Please feel free to open an issue or submit a pull request.
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.