Files
AI-Health/health_app/lib/core/local_database.dart
MingNian 6e69f1085e chore: 全面规范化代码,遵循 CLAUDE.md 编码规范
- C# 文件命名改为 snake_case(28 个文件重命名)
- C# 类转换为主构造函数(8 个类)
- 空 catch 添加异常类型(2 处)
- 新建 GlobalUsings.cs(Health.Infrastructure、Health.WebApi)
- Flutter 移除 go_router,改用 Riverpod 路由栈
- Flutter 移除 flutter_secure_storage,改用 sqflite 持久化
- 修复 Flutter 构建路径(Flutter SDK 迁至 D 盘)
- 后端端口改为 0.0.0.0:5000,支持局域网访问
2026-06-02 12:41:06 +08:00

59 lines
1.4 KiB
Dart

import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
/// SQLite 本地数据库——存储 token 等关键信息
class LocalDatabase {
static LocalDatabase? _instance;
Database? _db;
LocalDatabase._();
static LocalDatabase get instance => _instance ??= LocalDatabase._();
Future<Database> get database async {
_db ??= await _initDb();
return _db!;
}
Future<Database> _initDb() async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, 'health_app.db');
return openDatabase(
path,
version: 1,
onCreate: (db, version) async {
await db.execute(
'CREATE TABLE kv_store (key TEXT PRIMARY KEY, value TEXT)',
);
},
);
}
Future<void> write(String key, String value) async {
final db = await database;
await db.insert(
'kv_store',
{'key': key, 'value': value},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Future<String?> read(String key) async {
final db = await database;
final result =
await db.query('kv_store', where: 'key = ?', whereArgs: [key]);
if (result.isEmpty) return null;
return result.first['value'] as String?;
}
Future<void> delete(String key) async {
final db = await database;
await db.delete('kv_store', where: 'key = ?', whereArgs: [key]);
}
Future<void> deleteAll() async {
final db = await database;
await db.delete('kv_store');
}
}