GetStorage: Ultra-Fast and Lightweight Key-Value Storage for Multiple Platforms
Published on by Flutter News Hub
GetStorage is a highly optimized key-value storage solution suitable for Android, iOS, Web, Linux, Mac, Fuchsia, and Windows. Unlike traditional databases, GetStorage prioritizes blazing-fast memory access while ensuring data persistence.
Key Features
- Lightning-Fast: All operations, including writing, reading, and deleting, are performed instantaneously in memory.
- Persistent Backup: Data is automatically backed up to disk in the background after each operation.
- Multi-Platform Compatibility: Integrates seamlessly with multiple platforms, including mobile, desktop, and web.
- Data Types Supported: Store a wide range of data types, including strings, integers, doubles, maps, and lists.
- Easy Integration with GetX Framework: Leverages the GetX framework for simplified usage.
Installation
- Add to your pubspec.yaml:
dependencies:
get_storage: ^2.0.5
- Install the package:
$ flutter packages get
- Import the library:
import 'package:get_storage/get_storage.dart';
Usage
Initialize Storage Driver
main() async {
await GetStorage.init();
runApp(App());
}
Create a Storage Box
final box = GetStorage();
Write Data
box.write('quote', 'GetX is the best');
Read Data
print(box.read('quote')); // out: GetX is the best
Remove a Key
box.remove('quote');
Listen for Changes
Function? disposeListen;
disposeListen = box.listen(() {
print('box changed');
});
Dispose Listeners
disposeListen?.call();
Listen for Changes on a Specific Key
box.listenKey('key', (value) {
print('new key is $value');
});
Erase a Container
box.erase();
Create Custom Containers
GetStorage g = GetStorage('MyStorage');
SharedPreferences Implementation
class MyPref {
static final _otherBox = () => GetStorage('MyPref');
final username = ''.val('username');
final age = 0.val('age');
final price = 1000.val('price', getBox: _otherBox);
}
Benchmark Results
GetStorage consistently outperforms other storage solutions in terms of speed, making it ideal for applications that require real-time data access.
When to Use GetStorage
GetStorage is suitable for:
- Caching HTTP requests
- Storing simple user information
- Managing simple and persistent application state
- Replacing SharedPreferences
When Not to Use GetStorage
GetStorage is not recommended when:
- Indexing is required
- Explicit confirmation of data persistence to disk is necessary before proceeding with other operations
Conclusion
GetStorage is an indispensable tool for developers seeking a fast, lightweight, and persistent key-value storage solution. Its simplicity, cross-platform compatibility, and impressive performance make it a perfect choice for a wide range of applications.