Shared Preferences in Flutter
Store the data locally in Flutter app using Shared Preferences.
Shared Preferences is a kind of local storage that we can make use of to store smaller data inside Flutter App. The data in Shared Preferences are stored in the form of key value pair.
Video Tutorial
Dependencies
shared_preferences: ^0.5.12
Implementation
In this article we shall discuss how to store
, retrieve
and delete
the data using Shared Preferences.
- Storing data:
Let me store a String datatype locally using the shared preferences concept.
call the above defined function by passing the data <String> that we want to store locally saveMyName(“myName”)
Use setString()
to store the data. The setString()
accepts the key
and the Value
. In the above code snippet the name enclosed within codes "name"
is the key and the second parameter name
is the value.
2. Retrieve data:
Now let’s retrieve the data back what we have stored before.
For Retrieving the data we need to just pass in the key
alone. In our case the Key is name
. So we have to pass that to get the data back [Line no:4] from Shared Preferences. “Name not found
” [Line no:4] is a custom message that we show in case if there is no entry for that particular key in the local storage.
3. Deleting the data:
For deleting the particular entry, make use of remove
method and pass the key
alone as the parameter.
Just like String
we can also store Integer
and Bool
datatypes in Shared Preferences.
For Integer use., getInteger()
setInteger()
For Boolean use., getBool()
setBool()
👨💻Get the complete Source Code here 👉🏻: https://github.com/vijayinyoutube/sharedpreferences
Other articles you may like.,
🔵Setting up your Flutter app for publishing in Play Store.
🔴Confetti Animation in Flutter
🟡 Change App Launcher Icon in Flutter
🔴 AnimatedContainer Widget in Flutter
Flutter Tutorials
If you want to know more about Flutter and various Widgets in Flutter…?🤓 Then visit my channel vijaycreations🚩
Thanks.,