前言本文是基于官网最新稳固版本^2.0.8进行开发
源代码及视频教程地址源代码地址
视频教程地址
目标本文次要对shared_preferences: ^2.0.8的作用以及根本应用来进行源码剖析,最终会封装一个比拟通用的类库,因为2.0以上版本是空平安,所以前面讲的所有代码以及封装都是基于空平安的。
shared_preferences介绍shared_preferences次要的作用是用于将数据异步长久化到磁盘,因为长久化数据只是存储到长期目录,当app删除时该存储的数据就是隐没,web开发时革除浏览器存储的数据也将隐没。
反对存储类型:
boolintdoublestringstringListshared_preferences利用场景次要用于长久化数据,如长久化用户信息、列表数据等。
长久化用户信息因为用户信息根本是不扭转的,而在一个应用程序中经常会有多个页面须要展现用户信息,咱们不可能每次都去获取接口,那么本地长久化就会变得很不便。
长久化列表数据为了给用户更好的体验,在获取列表数据时咱们经常会先展现旧数据,带给用户更好的体验,不至于一关上页面就是空白的,当咱们采纳长久化列表数据后,能够间接先展现本地数据,当网络数据申请回来后在进行数据更新。
shared_preferences应用的对应类库咱们晓得每个平台长久化数据的形式都不一样,而shared_preferences针对不同的平台封装了一个通用的类库,接下来咱们看看不同平台下他们应用的库:
iOS: NSUserDefaultsAndroid: SharedPreferencesWeb: localStorageLinux: FileSystem(保留数据到本地系统文件库中)Mac OS: FileSystem(保留数据到本地系统文件库中)Windows: FileSystem(保留数据到本地系统文件库中)shared_preferences根本应用pubspec.yaml导入依赖shared_preferences: ^2.0.8导入头文件import 'package:shared_preferences/shared_preferences.dart';获取实例对象SharedPreferences? sharedPreferences = await SharedPreferences.getInstance();设置长久化数据咱们能够通过sharedPreferences的实例化对象调用对应的set办法设置长久化数据
SharedPreferences? sharedPreferences;// 设置长久化数据void _setData() async { // 实例化 sharedPreferences = await SharedPreferences.getInstance(); // 设置string类型 await sharedPreferences?.setString("name", "Jimi"); // 设置int类型 await sharedPreferences?.setInt("age", 18); // 设置bool类型 await sharedPreferences?.setBool("isTeacher", true); // 设置double类型 await sharedPreferences?.setDouble("height", 1.88); // 设置string类型的数组 await sharedPreferences?.setStringList("action", ["吃饭", "睡觉", "打豆豆"]); setState(() {});}读取长久化数据咱们能够通过sharedPreferences的实例化对象调用对应的get办法读取长久化数据
Text("名字: ${sharedPreferences?.getString("name") ?? ""}", style: TextStyle( color: Colors.blue, fontSize: 20 ), ),SizedBox(height: 20,),Text("年龄: ${sharedPreferences?.getInt("age") ?? ""}", style: TextStyle( color: Colors.red, fontSize: 20 ), ),SizedBox(height: 20,),Text("是老师吗?: ${sharedPreferences?.getBool("isTeacher") ?? ""}", style: TextStyle( color: Colors.orange, fontSize: 20 ), ),SizedBox(height: 20,),Text("身高: ${sharedPreferences?.getDouble("height") ?? ""}", style: TextStyle( color: Colors.pink, fontSize: 20 ), ),SizedBox(height: 20,),Text("我正在: ${sharedPreferences?.getStringList("action") ?? ""}", style: TextStyle( color: Colors.purple, fontSize: 20 ), ),残缺代码import 'package:flutter/material.dart';import 'package:shared_preferences/shared_preferences.dart';class SharedPreferencesExample extends StatefulWidget { @override _SharedPreferencesExampleState createState() => _SharedPreferencesExampleState();}class _SharedPreferencesExampleState extends State<SharedPreferencesExample> { SharedPreferences? sharedPreferences; // 设置长久化数据 void _setData() async { // 实例化 sharedPreferences = await SharedPreferences.getInstance(); // 设置string类型 await sharedPreferences?.setString("name", "Jimi"); // 设置int类型 await sharedPreferences?.setInt("age", 18); // 设置bool类型 await sharedPreferences?.setBool("isTeacher", true); // 设置double类型 await sharedPreferences?.setDouble("height", 1.88); // 设置string类型的数组 await sharedPreferences?.setStringList("action", ["吃饭", "睡觉", "打豆豆"]); setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("SharedPreferences"), ), floatingActionButton: FloatingActionButton( onPressed: _setData, child: Icon(Icons.add), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text("名字: ${sharedPreferences?.getString("name") ?? ""}", style: TextStyle( color: Colors.blue, fontSize: 20 ), ), SizedBox(height: 20,), Text("年龄: ${sharedPreferences?.getInt("age") ?? ""}", style: TextStyle( color: Colors.red, fontSize: 20 ), ), SizedBox(height: 20,), Text("是老师吗?: ${sharedPreferences?.getBool("isTeacher") ?? ""}", style: TextStyle( color: Colors.orange, fontSize: 20 ), ), SizedBox(height: 20,), Text("身高: ${sharedPreferences?.getDouble("height") ?? ""}", style: TextStyle( color: Colors.pink, fontSize: 20 ), ), SizedBox(height: 20,), Text("我正在: ${sharedPreferences?.getStringList("action") ?? ""}", style: TextStyle( color: Colors.purple, fontSize: 20 ), ), ], ), ), ); }}成果展现
...