 8299e1ad62
			
		
	
	
		8299e1ad62
		
	
	
	
	
		
			
			- 创建 Item 模型类,包含 toMap 和 fromMap 方法 - 新增 ItemRepository 类,负责数据库操作 - 实现 ItemScopedModel,用于状态管理 - 更新 SQLiteHelper,使用新的 item 表结构- 删除旧的 SQLiteOperation 文件
		
			
				
	
	
		
			39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:item_tracker/database/item_table.dart';
 | |
| import 'package:sqflite/sqflite.dart';
 | |
| import 'package:path/path.dart';
 | |
| import 'package:path_provider/path_provider.dart';
 | |
| 
 | |
| class DatabaseHelper {
 | |
|   // 数据库名称
 | |
|   static final _databaseName = "my_database.db";
 | |
|   // 数据库版本
 | |
|   static final _databaseVersion = 1;
 | |
| 
 | |
|   static final DatabaseHelper _instance = DatabaseHelper._internal();
 | |
|   factory DatabaseHelper() => _instance;
 | |
|   static Database? _database;
 | |
| 
 | |
|   DatabaseHelper._internal();
 | |
| 
 | |
|   Future<Database> get database async {
 | |
|     if (_database != null) return _database!;
 | |
|     return (await _initDatabase());
 | |
|   }
 | |
| 
 | |
|   // 初始化数据库
 | |
|   Future<Database> _initDatabase() async {
 | |
|     // 获取App文档目录
 | |
|     final directory = await getApplicationDocumentsDirectory();
 | |
|     final path = join(directory.path, _databaseName);
 | |
|     print('${path}');
 | |
|     return await openDatabase(
 | |
|       path,
 | |
|       version: _databaseVersion,
 | |
|       onCreate: _onCreate,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   Future<void> _onCreate(Database db, int version) async {
 | |
|     await db.execute(createItemTable);
 | |
|   }
 | |
| } |