An overview
- shared_preferences,It saves data in the form of Key-Value(key-value pair),supports Android and iOS
- shared_preferences is a third-party plug-in , use
SharedPreferences
in Android , useNSUserDefaults
- pub address:https://pub.flutter-io. cn/packages/shared_preferences
- Github address :https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
-
Open Device File Explorer,Select Device,Find the
data/data/com.example.flutter_image(package name)
/shared_prefs folder -
Right click the FlutterSharedPreferences.xml file to export& #xff0c; Open to view file information
Second add dependency
2.1 Dependency address
2.2 Add dependencies
Add dependencies in the pubspec.yaml
file of the project
dependencies:shared_preferences: ^2.0.6
Execute command
flutter pub get
Three shared_preferences operations
3.1 Supported data types
The data types supported by shared_preferences are int, double, bool, string, stringList
3.2 shared_preferences instantiation
Synchronous operation
Future _prefs = SharedPreferences.getInstance();
Asynchronous operation (async)
var prefs = await SharedPreferences.getInstance();
3.3 Basic operation of data (int as an example)
3.3.1 Read Data
Future _readData() async {var prefs = await SharedPreferences.getInstance();var result = ; prefs.getInt('Key_Int'); return result ?? 0;
}
3.3.2 Save data
_saveData() async {var prefs = await SharedPreferences.getInstance();prefs.setInt('Key_Int', 12);
}
3.3.3 Delete data
Future _deleteData() async {var prefs = await SharedPreferences.getInstance();prefs .remove('Key');
}
3.3.4 Clear all data
Future _clearData() async {var prefs = await SharedPreferences.getInstance();prefs.clear();
}
3.4 Key related operations
3.4.1 Get all Keys
Future<Set> _getKeys() async {var prefs = await SharedPreferences .getInstance();var keys = prefs.getKeys();return keys ?? [];
}
3.4.2 Check whether Does the Key exist
Future _containsKey() async {var prefs = await SharedPreferences.getInstance();return prefs.containsKey('Key') ? ? false;
}
Four examples
4.1 Code
Future _prefs = SharedPreferences.getInstance(); //sharepreference initialization
late Future _counter;//variables//in turn +1Future _incrementCounter() async {final SharedPreferences prefs & #61; await _prefs; final int counter = (prefs.getInt('counter') ?? 0) + 1;setState(() {_counter = prefs.setInt ("counter", counter).then((bool success) {return counter;});});}@override void initState() {super.initState();_counter = _prefs.then(( SharedPreferences prefs) {return (prefs.getInt('counter') ?? 0);});}body: Center(child: FutureBuilder(future: _counter,builder: (BuildContext context, AsyncSnapshot snapshot) {switch (snapshot.connectionState) {case ConnectionState.waiting:return const CircularProgressIndicator();default:if (snapshot.hasError) {return Text('Error: ${snapshot.error}'Error: ${snapshot.error}' 39;);} else {return Text('Button clicked ${snapshot.data} times',);}}})), floatingActionButton: FloatingActionButton(onPressed: _incrementCounter, tooltip: ' ;Increment',child: const Icon(Icons.add),),