Using SharedPreferences in Flutter effortlessly

We all know that SharedPreferences is a key/value store where you can read and store data very easily. It’s being used in most apps nowadays.
In Flutter, there’s a package named shared_preferences that helps us
deal with key/value data. Its documentation gives us something like this:
1 | _incrementCounter() async { |
It is really inconvenient to get the SharedPreferences instance asynchronously every time we need it. To increase reusability and reduce boilerplate code, I have an approach to save the instance of SharedPreferences and create getters/setters for the preference keys to use them anywhere in the app.
“Talk is cheap. Show me the code.” ~ Linus Torvalds
I will assume that you have already added the shared_preferences package.
First, we need to define a class to store the SharedPreferences instance. Let’s name it SharedPrefs.
1 | // utils/shared_prefs.dart |
Easy, right?
Then, we will define an init() method to get the SharedPreference instance and store it to the _sharedPrefs variable in the SharedPrefs class.
1 | // utils/shared_prefs.dart |
And call it in main() function.
1 | // main.dart |
Why called SharedPrefs.init() in the main() function? Because before runApp() is called, the app will show the splash screen. It’s a perfect moment for us to do some essential initialization for the app.
The next step is to define getters & setters for your preference keys. For example, I defined a getter and setter for getting & updating username in SharedPreferences.
1 | // utils/shared_prefs.dart |
I also create a keyUsername constant for consistency. You will not want to use a String directly like _sharedPrefs.getString(“key_username”) and in another place use _sharedPrefs.setString(“key_user_name”, value) by mistake.
That’s it. Now you can access username ANYWHERE in the app.
1 | class MyApp extends StatelessWidget { |
Thank you for reading my blog. You can check out this gist for the full code.
Have questions? Find me at https://blog.codingteemo.me