Flutter本地存储插件simple_storage_plugin的使用
Flutter本地存储插件simple_storage_plugin的使用
simple_storage_plugin
是一个用于在Android设备上使用SharedPreferences存储数据的插件。它高度依赖于 simple_signing_plugin
。存储的数据将使用AES进行加密。为了确保包能够正常工作,需要保护设备的屏幕锁。
特性
该插件包含以下方法:
writeData
- 将用户输入和密钥保存到SharedPreferences中。执行此操作需要身份验证。readData
- 从SharedPreferences中读取保存在指定键下的数据。如果不存在这样的键,则会抛出异常。deleteData
- 从SharedPreferences中删除保存在指定键下的数据。editData
- 编辑保存在SharedPreferences中指定键下的数据。执行此操作需要身份验证。
使用示例
// 写入数据示例
String _data = '数据';
String _key = '键';
var result = await SimpleStoragePlugin.writeData(_key, _data); // 如果一切顺利返回true。可能会抛出SharedPreferencesException或DeviceNotSecuredException
// 读取数据示例
String _key = '键';
var result = await SimpleStoragePlugin.readData(_key); // 如果一切顺利返回写入的数据。可能会抛出InvalidSignatureException, DeviceNotSecuredException或NoKeyInStorageException
// 删除数据示例
String _key = '键';
var result = await SimpleStoragePlugin.deleteData(_key); // 如果一切顺利返回true。可能会抛出SharedPreferencesException或DeviceNotSecuredException
// 编辑数据示例
String _data = '数据';
String _key = '键';
var result = await SimpleStoragePlugin.editData(_key, _data); // 如果一切顺利返回true。可能会抛出SharedPreferencesException或DeviceNotSecuredException
完整示例Demo
以下是完整的示例代码,展示了如何在Flutter应用中使用 simple_storage_plugin
插件。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:simple_storage_plugin/simple_storage_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TextEditingController writeKeyController = TextEditingController();
TextEditingController writeDataController = TextEditingController();
TextEditingController readKeyController = TextEditingController();
TextEditingController deleteKeyController = TextEditingController();
TextEditingController editKeyController = TextEditingController();
TextEditingController editDataController = TextEditingController();
String writeResult = '';
String readResult = '';
String deleteResult = '';
String editResult = '';
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('存储示例应用'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: [
const Text(
'1. 写入',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
),
TextFormField(
controller: writeKeyController,
decoration: const InputDecoration(hintText: "键"),
),
TextFormField(
controller: writeDataController,
decoration: const InputDecoration(hintText: "数据"),
),
RawMaterialButton(
onPressed: () async {
if (writeDataController.text.isNotEmpty &&
writeKeyController.text.isNotEmpty) {
var result = await SimpleStoragePlugin.writeData(
writeKeyController.text, writeDataController.text);
if (result == true) {
setState(() {
writeResult = '成功!';
});
} else {
setState(() {
writeResult = '失败!';
});
}
}
},
child: const Text('写入!'),
),
Text(writeResult),
const Divider(),
const Text(
'2. 读取',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
),
TextFormField(
controller: readKeyController,
decoration: const InputDecoration(hintText: "键"),
),
RawMaterialButton(
onPressed: () async {
if (readKeyController.text.isNotEmpty) {
var result = await SimpleStoragePlugin.readData(
readKeyController.text);
if (result != false) {
setState(() {
readResult = result;
});
} else {
setState(() {
readResult = '失败!';
});
}
}
},
child: const Text('读取!'),
),
Text(readResult),
const Divider(),
const Text(
'3. 删除',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
),
TextFormField(
controller: deleteKeyController,
decoration: const InputDecoration(hintText: "键"),
),
RawMaterialButton(
onPressed: () async {
if (deleteKeyController.text.isNotEmpty) {
var result = await SimpleStoragePlugin.deleteData(
deleteKeyController.text);
if (result == true) {
setState(() {
deleteResult = "已删除!";
});
} else {
setState(() {
deleteResult = "失败!";
});
}
}
},
child: const Text('删除!'),
),
Text(deleteResult),
const Divider(),
const Text(
'4. 编辑',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
),
TextFormField(
controller: editKeyController,
decoration: const InputDecoration(hintText: "键"),
),
TextFormField(
controller: editDataController,
decoration: const InputDecoration(hintText: "数据"),
),
RawMaterialButton(
onPressed: () async {
if (editDataController.text.isNotEmpty &&
editKeyController.text.isNotEmpty) {
var result = await SimpleStoragePlugin.editData(
editKeyController.text, editDataController.text);
if (result == true) {
setState(() {
editResult = '成功!';
});
} else {
setState(() {
editResult = '失败!';
});
}
}
},
child: const Text('编辑!'),
),
Text(editResult),
const Divider(),
],
)),
),
),
);
}
}
更多关于Flutter本地存储插件simple_storage_plugin的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter本地存储插件simple_storage_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
simple_storage_plugin
是一个用于 Flutter 的本地存储插件,它提供了一种简单的方式来存储和检索数据。以下是使用 simple_storage_plugin
的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 simple_storage_plugin
依赖:
dependencies:
flutter:
sdk: flutter
simple_storage_plugin: ^0.0.1 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在你的 Dart 文件中导入 simple_storage_plugin
:
import 'package:simple_storage_plugin/simple_storage_plugin.dart';
3. 初始化插件
在使用插件之前,建议先初始化它。通常你可以在 main()
函数中进行初始化:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SimpleStoragePlugin.init();
runApp(MyApp());
}
4. 存储数据
你可以使用 SimpleStoragePlugin
来存储数据。以下是一些常见的存储操作:
存储字符串
await SimpleStoragePlugin.saveString('key', 'value');
存储整数
await SimpleStoragePlugin.saveInt('key', 123);
存储布尔值
await SimpleStoragePlugin.saveBool('key', true);
存储 double
await SimpleStoragePlugin.saveDouble('key', 123.45);
存储列表
await SimpleStoragePlugin.saveStringList('key', ['value1', 'value2']);
5. 读取数据
你可以使用 SimpleStoragePlugin
来读取之前存储的数据:
读取字符串
String value = await SimpleStoragePlugin.getString('key');
读取整数
int value = await SimpleStoragePlugin.getInt('key');
读取布尔值
bool value = await SimpleStoragePlugin.getBool('key');
读取 double
double value = await SimpleStoragePlugin.getDouble('key');
读取列表
List<String> value = await SimpleStoragePlugin.getStringList('key');
6. 删除数据
你可以删除存储在特定键下的数据:
await SimpleStoragePlugin.remove('key');
7. 清除所有数据
你也可以清除所有存储的数据:
await SimpleStoragePlugin.clear();
8. 检查键是否存在
你可以检查某个键是否已经存储了数据:
bool exists = await SimpleStoragePlugin.containsKey('key');
9. 获取所有键
你可以获取所有已存储的键:
List<String> keys = await SimpleStoragePlugin.getKeys();
10. 示例
以下是一个简单的示例,展示了如何使用 simple_storage_plugin
来存储和读取数据:
import 'package:flutter/material.dart';
import 'package:simple_storage_plugin/simple_storage_plugin.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SimpleStoragePlugin.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: StorageExample(),
);
}
}
class StorageExample extends StatefulWidget {
[@override](/user/override)
_StorageExampleState createState() => _StorageExampleState();
}
class _StorageExampleState extends State<StorageExample> {
String _storedValue = '';
Future<void> _saveData() async {
await SimpleStoragePlugin.saveString('myKey', 'Hello, Flutter!');
}
Future<void> _readData() async {
String value = await SimpleStoragePlugin.getString('myKey');
setState(() {
_storedValue = value;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Storage Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Stored Value: $_storedValue'),
SizedBox(height: 20),
ElevatedButton(
onPressed: _saveData,
child: Text('Save Data'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _readData,
child: Text('Read Data'),
),
],
),
),
);
}
}