Example of Shared Preferences in Flutter to Save Data Locally

Hello friends, In today’s tutorial we would learn about Shared Preferences in flutter. Many of us don’t know about Shared Preferences, How it works, What it does and where can we use Shared Preferences. We would discuss all these topics in our tutorial. Now the main part is know the job of it. Shared Preferences is use to store a small amount of data locally in application memory using a Unique KEY. We can also retrieve the stored data from that KEY and also delete it if we required. The good thing is that it stores the data permanently in app’s memory so unless we remove the app or delete the data using its KEY, It will not be delete. So in this tutorial we would learn about Example of Shared Preferences in Flutter to Save Data Locally.

Question : Where we can use Shared Preferences ?

Answer : We can use it to store Entered Username and Password locally using Save me button. So there is no need to enter email password each time we login. We can also store app login season cookie in it.

Write Data in Shared Preferences :-

Read Data in Shared Preferences :-

Contents in this project Example of Shared Preferences in Flutter to Save Data Locally :-

1. First of all we have to install shared_preferences 2.0.13 Pub package in our flutter project. So open your flutter project Root directory in Command Prompt or Terminal and execute below command.

Screenshot after done installation :-

2. Now we’re ready to go. Open your project’s main.dart file and import material.dart, dart:async and shared_preferences.dart package.

3. Creating void main runApp() method and here we would call our MyApp class.

4. Creating MyApp extends StatelessWidget. In this class we would call our Child() class.

5. Creating Child extends StatefulWidget class. In this class we would make createState() method and make our main Home class.

6. Creating our main Home extends State<Child> class.

7. Creating a TextEditingController object named as T_controller. It is used to get TextField entered value.

8. Creating a SharedPreferences object named as s_prefs.

9. Creating a String variable named as temp with empty value. It is used to store the saved shared preferences key and show into Text widget.

10. Creating a ASYNC type of function named as _saveToShared_Preferences. In this function we would first make Instance of shared preferences. Now save the TextField entered value using KEY.

11. Creating another function named as _showSavedValue(). In this function we would retrieve the locally save value using the Key again and store into temp variable.

12. Creating another function named as _delete_Shared_Preferences(). In this function we would delete the saved value.

13. Creating Widget Build area -> Now we would make 1 TextField widget, 1 Text widget and 3 Button widgets.

14. Complete source code for main.dart file :-

Screenshots :-

Example of Shared Preferences in Flutter to Save Data Locally

Leave a Reply

Your email address will not be published.