What is get_storage?
get_storage is a persistent key/value storage for Android, iOS, Web, Linux, Mac, Fuchsia, and Windows, that combines fast memory access with persistent storage.
Why it is required?
There may come a time when you want to persist data in your Flutter app so you can reuse it later. A common use case for this functionality is storing login credentials to be recalled the next time the user launches the app.
With get_storage, you can configure your Flutter app to remember the data even after the user terminates their activity. This can be used to store critical data such as passwords, tokens, and complex relational data.
Read more about Flutter app development services from mobikul.
In this blog, we’ll demonstrate how to persist and modify data using get_storage in a Flutter app.
Support
Platform– ANDROID, IOS, LINUX, MACOS, WEB, WINDOWS
Datatypes– String, int, double, Map, and List
Features
- Easy to use
- Null Safety
- No need to use await while reading / Writing values
- Provide listeners on specific keys or on complete box
Let’s start the implementation
Add the dependencies in pubspec.yaml file:
1- Add the dependencies in pubspec.yaml file:
1 2 |
dependencies: get_storage: ^2.0.3 |
2- Initialization
To Initialize storage, you must call GetStorage.init() method before loading the app / before calling runApp.
1 2 3 4 |
void main() async { await GetStorage.init(); //get storage initialization runApp(MyApp()); } |
3- Basic Usage
Create an instance of GetStorage
1 |
final datacount = GetStorage(); // instance of getStorage class |
How to store/write data in get storage
1 |
datacount.write("count", 10); |
How to read data?
1 |
print("Current Count Value: ${datacount.read('count')}"); |
As a result, it will print the value of the count
1 |
Current Count Value: 10 |
How to delete a value or clear the complete box
1 2 3 |
datacount.remove('count'); //----It will remove the key count from storage datacount.erase(); //-----It will erase all te data from dataCount Box |
4- Advanced Usage
Now, we will learn the advanced usage of get storage.
Named Box
Sometimes, in the application, we are required to save data that is not interrelated
For example:- Data related to the user only or Data related to the application.
For solving the above-mentioned issue we can create 2 named boxes that deal with the data accordingly.
1 2 3 4 |
GetStorage userStorage = GetStorage("userRelatedData"); //---Use if info related to user only GetStorage globalStorage = GetStorage("globalStorage"); //-----Use if info not dependent on user |
Now, we can use the named boxes
1 |
globalStorage.write('themeMode', isDarkTheme);//--If not related to user userStorage.write('userLogin',loginCredentials); //--If user relatd only |
Keys / Box Listeners
1 2 3 4 5 6 7 |
userStorage.listen((){ print('box changed'); }); globalStorage.listenKey('themeMode', (value){ print('Key Changed') }); |
For more details, please check Get Storage
Hopefully, this blog will be helpful to you. If you have any queries, please write them in the comment section.