1024programmer News Flutter development – data persistent storage shared_preferences

Flutter development – data persistent storage shared_preferences

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 , use NSUserDefaults
  • Second add dependency

    2.1 Dependency address

    • pub address:https://pub.flutter-io. cn/packages/shared_preferences
    • Github address :https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences

    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 &#61 ; 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: &#39 ;Increment',child: const Icon(Icons.add),),

    4.2 renderings

    4.3 View shared_preferences file

    • 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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/flutter-development-data-persistent-storage-shared_preferences/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索