|
| 1 | +# Table of Contents |
| 2 | + |
| 3 | +- [Available Backends](#available-backends) |
| 4 | +- [In-Memory Backend](#in-memory-backend) |
| 5 | +- [Shared Preferences Backend](#shared-preferences-backend) |
| 6 | +- [Hive Backend](#hive-backend) |
| 7 | +- [Secure Storage Backend](#secure-storage-backend) |
| 8 | +- [Custom Backends](#custom-backends) |
| 9 | +- [Using Multiple Backends](#using-multiple-backends) |
| 10 | + |
| 11 | +# Available Backends |
| 12 | + |
| 13 | +## In-Memory Backend: |
| 14 | + |
| 15 | +A simple, built-in, in-memory backend for temporary storage. This backend is useful for testing and scenarios where data |
| 16 | +persistence is not required. Data stored in this backend will be lost when the application is closed or restarted. |
| 17 | + |
| 18 | +```dart |
| 19 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 20 | +
|
| 21 | +void main() async { |
| 22 | + final storage = await HyperStorage.init(backend: InMemoryBackend()); |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +> In memory backend is backed by a simple `Map`. It is not suitable for production use cases where data persistence is |
| 27 | +> required. |
| 28 | +
|
| 29 | +## [Shared Preferences Backend](https://pub.dev/packages/hyper_storage_shared_preferences): |
| 30 | + |
| 31 | +A persistent storage backend using SharedPreferences, which is ideal for storing small amounts of data in a key-value |
| 32 | +format. This backend is commonly used for user preferences and settings. This is an ideal choice for applications that |
| 33 | +need to store simple data that is also accessible outside the app (e.g., user preferences) and do not require high |
| 34 | +security. For example, sharing data between Flutter and native Android/iOS code for various use-cases like widgets, |
| 35 | +live activities, native runners, etc. |
| 36 | + |
| 37 | +```dart |
| 38 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 39 | +import 'package:hyper_storage_shared_preferences/hyper_storage_shared_preferences.dart'; |
| 40 | +
|
| 41 | +void main() async { |
| 42 | + final storage = await HyperStorage.init(backend: SharedPreferencesBackend()); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +> Hyper Storage uses `SharedPreferencesAsync` API under the hood, which is a fully asynchronous API for |
| 47 | +> `SharedPreferences`. |
| 48 | +
|
| 49 | +## [Hive Backend](https://pub.dev/packages/hyper_storage_hive): |
| 50 | + |
| 51 | +A persistent storage backend using Hive, a lightweight and fast key-value database written in pure Dart. This backend |
| 52 | +is suitable for storing larger amounts of data and supports complex data types. |
| 53 | + |
| 54 | +```dart |
| 55 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 56 | +import 'package:hyper_storage_hive/hyper_storage_hive.dart'; |
| 57 | +
|
| 58 | +void main() async { |
| 59 | + final storage = await HyperStorage.init(backend: HiveBackend()); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +The package also provides a `LazyHiveBackend`, which initializes Hive lazily. This is useful in scenarios where you want |
| 64 | +to defer the initialization of Hive until it's actually needed, potentially improving startup performance. |
| 65 | + |
| 66 | +```dart |
| 67 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 68 | +import 'package:hyper_storage_hive/hyper_storage_hive.dart'; |
| 69 | +
|
| 70 | +void main() async { |
| 71 | + // Initialize Hive before using LazyHiveBackend. |
| 72 | + await Hive.initFlutter(); |
| 73 | + final storage = await HyperStorage.init(backend: LazyHiveBackend()); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +> Hive backend requires initialization of Hive before use. Hyper Storage do not handle Hive initialization for you. |
| 78 | +> You need to initialize Hive in your application before using the Hive backend. For example: |
| 79 | +
|
| 80 | +## [Secure Storage Backend](https://pub.dev/packages/hyper_secure_storage): |
| 81 | + |
| 82 | +A secure storage backend using [`flutter_secure_storage`](https://pub.dev/packages/flutter_secure_storage), which |
| 83 | +provides a way to store sensitive data securely. This backend is ideal for storing credentials, tokens, and other |
| 84 | +sensitive information. |
| 85 | + |
| 86 | +```dart |
| 87 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 88 | +import 'package:hyper_secure_storage/hyper_secure_storage.dart'; |
| 89 | +
|
| 90 | +void main() async { |
| 91 | + final storage = await HyperStorage.init(backend: SecureStorageBackend()); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +You can optionally provide an instance `FlutterSecureStorage` to the `SecureStorageBackend` constructor to customize its |
| 96 | +behavior. |
| 97 | + |
| 98 | +```dart |
| 99 | +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 100 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 101 | +
|
| 102 | +import 'package:hyper_secure_storage/hyper_secure_storage.dart'; |
| 103 | +
|
| 104 | +void main() async { |
| 105 | + final secureStorage = FlutterSecureStorage( |
| 106 | + aOptions: AndroidOptions(encryptedSharedPreferences: true), |
| 107 | + iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock), |
| 108 | + ); |
| 109 | +
|
| 110 | + final storage = await HyperStorage.init( |
| 111 | + backend: SecureStorageBackend(secureStorage: secureStorage), |
| 112 | + ); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Custom Backends |
| 117 | + |
| 118 | +Hyper Storage is designed to be extensible, allowing you to create your own custom backends by implementing the |
| 119 | +`StorageBackend` interface. This enables you to integrate with any storage solution that fits your application's |
| 120 | +requirements. |
| 121 | + |
| 122 | +```dart |
| 123 | +// Define your custom backend by implementing the StorageBackend interface. |
| 124 | +class YourCustomBackend implements StorageBackend { |
| 125 | + // Implement all required methods here. |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Then, you can use your custom backend with Hyper Storage as follows: |
| 130 | + |
| 131 | +```dart |
| 132 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 133 | +import 'package:your_custom_backend/your_custom_backend.dart'; |
| 134 | +
|
| 135 | +void main() async { |
| 136 | + final storage = await HyperStorage.init(backend: YourCustomBackend()); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Using Multiple Backends |
| 141 | + |
| 142 | +You can also use multiple backends in your application by creating separate `HyperStorage` instances for each backend. |
| 143 | + |
| 144 | +```dart |
| 145 | +import 'package:hyper_storage/hyper_storage.dart'; |
| 146 | +import 'package:hyper_storage_shared_preferences/hyper_storage_shared_preferences.dart'; |
| 147 | +import 'package:hyper_storage_hive/hyper_storage_hive.dart'; |
| 148 | +
|
| 149 | +void main() async { |
| 150 | + final sharedPreferencesStorage = await HyperStorage.newInstance(backend: SharedPreferencesBackend()); |
| 151 | + final hiveStorage = await HyperStorage.newInstance(backend: HiveBackend()); |
| 152 | +
|
| 153 | + // Use sharedPreferencesStorage for user preferences |
| 154 | + await sharedPreferencesStorage.setString('theme', 'dark'); |
| 155 | +
|
| 156 | + // Use hiveStorage for app data |
| 157 | + await hiveStorage.setString('user_data', 'some complex data'); |
| 158 | +} |
| 159 | +``` |
0 commit comments