ToneSnap_FSX_Flutter/lib/data/storage/my_voice_data.dart
fengshengxiong 422a3f8802 first commit
2024-06-11 11:53:38 +08:00

46 lines
1019 B
Dart

// Author: fengshengxiong
// Date: 2024/5/8
// Description: 收藏数据
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/data/storage/hive_storage.dart';
class MyVoiceData {
/// 私有构造函数
MyVoiceData._();
/// 静态常量用于保存类的唯一实例
static final MyVoiceData _instance = MyVoiceData._();
/// 工厂构造函数返回类的唯一实例
factory MyVoiceData() {
return _instance;
}
/// 声明盒子
/// 注意, main函数中这个盒子已经打开, 可以进行存储操作
final _box = getMyVoiceBox();
/// 获取数据
List<VoiceModel> getList() {
return _box.values.toList();
}
/// 添加数据
Future<int> addData(VoiceModel voiceModel) async {
return await _box.add(voiceModel);
}
/// 删除
Future<void> delete(index) async {
await _box.deleteAt(index);
await _box.flush();
}
/// 清空所有数据
Future<void> clear() async {
await _box.clear();
await _box.flush();
}
}