What is GetStorage?
GetStorage is a simple, fast, and lightweight key-value storage solution that comes with GetX. It stores data in a JSON file on the device (Android, iOS, Web, Desktop) and provides a synchronous API with reactive capabilities. It's perfect for storing user preferences, app settings, small caches, or any small amount of data that needs to persist across app launches.
Why Use GetStorage?
- No context needed – Can be used anywhere in your app, including controllers and services.
- Simple API –
write,read,delete,hasData– very straightforward. - Reactive – GetStorage can be observed with
.obsor workers, so UI updates automatically. - Lightweight – Stores data in a single JSON file; no complex setup.
- Cross-platform – Works on Android, iOS, Web, macOS, Windows, Linux.
- Synchronous – No
async/awaitfor simple operations (though writing is async behind the scenes).
Installation
GetStorage is part of the GetX package. Just add GetX to your pubspec.yaml.
Initialization
Before using GetStorage, you need to initialize it. It's recommended to do this in main() before runApp.
You can also create named storage boxes by passing a name.
Basic Usage
Working with Named Boxes
You can create multiple isolated storage boxes using names. This helps organize data by module.
Reactive Storage
GetStorage can be observed just like any reactive variable. You can make a GetStorage instance observable by calling .obs or use workers to react to changes.
Using GetStorage with GetBuilder/Obx
Storage Service with GetxService
For better organization, encapsulate storage logic in a GetxService that can be used throughout the app.
Comparison with Other Storage Options
Best Practices
- Initialize in
main– Callawait GetStorage.init()beforerunAppto ensure storage is ready. - Use named boxes for modules – Keep data separate (e.g.,
user,app,cache). - Encapsulate storage in services – Makes it easier to test and change underlying storage later.
- Use reactive patterns – Combine with
Obxfor automatic UI updates when preferences change. - Provide defaults – Use the
??operator when reading to avoid null errors. - Avoid storing large data – GetStorage is not meant for large files or complex relational data; use SQLite or Hive for that.
Common Mistakes
- ❌ Forgetting to initialize – Using
GetStorage()beforeinit()will throw an exception. ✅ Always callGetStorage.init()first. - ❌ Using
readwithout checking existence – Can return null; handle with??orhasData. - ❌ Storing custom objects directly – GetStorage only supports primitive types and lists/maps. Convert custom objects to Map first.
- ❌ Using async/await unnecessarily –
writeis actually synchronous (it writes to disk on a separate thread), so you don't need to await it unless you want to ensure it's written before proceeding.
FAQ
- Q: Is GetStorage persistent?
A: Yes, it persists data across app restarts. Data is stored in a JSON file on the device. - Q: Can I store custom objects?
A: Not directly. You need to convert them toMap(e.g.,{'name': user.name, 'age': user.age}) and then store. When reading, convert back. - Q: How do I listen to changes in GetStorage?
A: Wrap the storage read in anObxor use workers on a reactive variable that holds the value. The storage itself doesn't have listeners, but you can combine it with reactive variables. - Q: What's the maximum storage size?
A: It's limited by the file system, but for large data (e.g., >10MB) consider using Hive or SQLite. - Q: Can I use GetStorage on the web?
A: Yes, it useslocalStorageon web, and the API is identical. - Q: How to delete all data from a box?
A: Usebox.erase().
Conclusion
GetStorage is a simple, powerful solution for persistent key-value storage in Flutter. It integrates seamlessly with GetX's state management and dependency injection, making it easy to manage app preferences and cached data. With its straightforward API and reactive capabilities, it's an excellent choice for most Flutter apps.