1、shared_preferences 插件
- 类似于 Android 中的 SharedPreferences,键值对的形式进行本地存储
1.1、引入插件
在 pubspec.yaml 中引入
dart
dependencies:
shared_preferences: ^2.1.0
在需要用到的文件中引入
dart
import 'package:shared_preferences/shared_preferences.dart';
1.2、基本用法
- 写入数据
dart
var sp = await SharedPreferences.getInstance();
sp.setString('name', 'Leon');
- 读取数据
dart
var sp = await SharedPreferences.getInstance();
var name = sp.getString('name');
- 删除数据
dart
var sp = await SharedPreferences.getInstance();
sp.remove('name');
2、代码示例
- 工具类
dart
import 'package:shared_preferences/shared_preferences.dart';
class SharedPreferencesUtils {
static saveData<V>(String key, V value) async {
var sp = await SharedPreferences.getInstance();
var type = value.runtimeType;
if (type == int) {
sp.setInt(key, value as int);
} else if (type == double) {
sp.setDouble(key, value as double);
} else if (type == String) {
sp.setString(key, value as String);
} else if (type == bool) {
sp.setBool(key, value as bool);
} else {
print('类型不匹配');
}
}
static getData(String key) async {
var sp = await SharedPreferences.getInstance();
print("sp.get:${sp.get(key)}");
return sp.get(key);
}
static deleteAllData() async {
var sp = await SharedPreferences.getInstance();
for (String key in sp.getKeys()) {
sp.remove(key);
}
}
}
- 页面代码
dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
const String KEY_NAME = "name";
const String KEY_AGE = "age";
const String KEY_WEIGHT = "weight";
const String KEY_BOOL = "bool";
class SpDemo extends StatefulWidget {
const SpDemo({super.key});
@override
State<SpDemo> createState() => _SpDemoState();
}
class _SpDemoState extends State<SpDemo> {
var _resultShow = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('SpDemo'),
),
body: Column(
children: [
ElevatedButton(onPressed: _saveData2Sp, child: const Text('保存数据')),
ElevatedButton(onPressed: _getDataFromSp, child: const Text('获取数据')),
ElevatedButton(onPressed: _deleteDataInSp, child: const Text('删除数据')),
Text(_resultShow),
],
),
);
}
void _saveData2Sp() async {
await SharedPreferencesUtils.saveData(KEY_NAME, 'Leon');
await SharedPreferencesUtils.saveData(KEY_AGE, 26);
await SharedPreferencesUtils.saveData(KEY_WEIGHT, 60.5);
await SharedPreferencesUtils.saveData(KEY_BOOL, true);
setState(() {
_resultShow = "保存成功";
});
}
void _getDataFromSp() async {
var name = await SharedPreferencesUtils.getData(KEY_NAME);
var age = await SharedPreferencesUtils.getData(KEY_AGE);
var weight = await SharedPreferencesUtils.getData(KEY_WEIGHT);
var b = await SharedPreferencesUtils.getData(KEY_BOOL);
print('姓名:$name\n年龄:$age\n体重:$weight\n其他:$b');
setState(() {
_resultShow = "姓名:$name\n年龄:$age\n体重:$weight\n其他:$b";
});
}
void _deleteDataInSp() async {
await SharedPreferencesUtils.deleteAllData();
setState(() {
_resultShow = "删除成功";
});
}
}
dart
import 'package:flutter/material.dart';
import 'SharedPreferencesDemo.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Leon Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SpDemo(),
);
}
}