Shared preferences are used to store the small amount of data like some int, double, bool, or string value in the local memory. Let me take a simple use case of it for your better understanding. Keep the user logged in using shared preferences This is one of the best use cases of shared preferences. Once the user gives the correct id and password then we save a bool value equal to true in local memory. Let’s say isUserLoggedIn = true. Now you can check the value of isUserLoggedIn whenever the user starts the app and based on the value you can show the login screen or main dashboard directly. Now you can check the value of isUserLoggedIn whenever the user starts the app and based on the value you can show the login screen or main dashboard directly. What we are going to build? To demonstrate the use of shared preferences in Flutter we will develop a simple app in which you can increase or decrease the number shown in the text filed with the help of these two buttons. Once you set the desired value then you can terminate the app and restart it again. You will find the same value in the text field because of . shared preferences Let's get started with the coding part!!! Create a new Flutter project Create a new . You can use your existing project also if you have any. Flutter project using the android studio Get the plugin We will be using this for implementing Shared Preferences. It is the wrapper of in android and in iOS. plugin SharedPreferences UserDefaults Go to the pubspec.yaml and paste the dependency like this. Now you have to import this plugin in every file where you want to use it. ; import 'package:shared_preferences/shared_preferences.dart' Now you have the concept of writing and reading the data from local memory in Flutter, so let’s develop this app. Here I am not talking about the UI part because our main focus is on the logic. The integer value in the text field is coming from the saved preferences. When you click on the plus button, I am just increasing the current integer value by 1 and saving the new value in the memory. RaisedButton(onPressed: () { setState(() { number++; }); saveIntInLocalMemory( , number); }, : Text( ), ) 'COUNTER_NUMBER' child '+' Similarly, when you are pressing the minus button, I am decreasing the current integer value by 1 and saving the new value to the memory. RaisedButton(onPressed: (){ setState(() { number--; }); saveIntInLocalMemory( , number); }, : Text( ), ) 'COUNTER_NUMBER' child '-' Complete Source code ; ; main() { runApp(MyApp()); } { @override Widget build(BuildContext context) { MaterialApp( title: , : ThemeData( primarySwatch: Colors.blue, : VisualDensity.adaptivePlatformDensity, ), : MyHomePage(), ); } } { MyHomePage({Key key}) : (key: key); @override _MyHomePageState createState() => _MyHomePageState(); } { number = ; @override initState() { .initState(); getIntFromLocalMemory( ).then( number = value ); } @override Widget build(BuildContext context) { Scaffold( appBar: AppBar( title: Text( ), ), : Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, : [ RaisedButton(onPressed: (){ setState(() { number--; }); saveIntInLocalMemory( , number); }, : Text( ), ), Padding( padding: EdgeInsets.all( ), : Text(number.toString()), ), RaisedButton(onPressed: () { setState(() { number++; }); saveIntInLocalMemory( , number); }, : Text( ), ) ], ), ) ); } Future<int> getIntFromLocalMemory( key) { pref = SharedPreferences.getInstance(); number = pref.getInt(key) ?? ; number; } Future< > saveIntInLocalMemory( key, int value) { pref = SharedPreferences.getInstance(); pref.setInt(key, value); } } import 'package:flutter/material.dart' import 'package:shared_preferences/shared_preferences.dart' void class MyApp extends StatelessWidget // This widget is the root of your application. return 'Flutter Demo' theme visualDensity home class MyHomePage extends StatefulWidget super < > class _MyHomePageState extends State MyHomePage var 0 void super 'COUNTER_NUMBER' ( ) => value return 'Save data locally' body children 'COUNTER_NUMBER' child '-' const 21.0 child 'COUNTER_NUMBER' child '+' /* * It saves the int value to the local memory. * */ String async var await var 0 return /* * It returns the saved the int value from the memory. * */ void String async var await Conclusion I hope this blog post is useful for you, do let me know your opinion in . community This article is originally published at: warmodroid's blog