首先在 pubspec.yaml 文件中添加 shared_preferences 引用并 Packages get
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
shared_preferences: ^0.4.2
然后封装 SharedPreferences 工具类
import 'package:shared_preferences/shared_preferences.dart';
/**
* @des SharedPreferences 工具类
* @author liyongli 20190522
* */
class SharedPreferencesDataUtils{
// 保存数据
static Future setUserInfo(String key, value) async{
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setString(key, value);
}
// 获取数据
static Future<String> getUserInfo(String key) async{
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getString(key);
}
// 清除数据
static Future deleteUserInfo(String key) async{
SharedPreferences sp = await SharedPrefere