import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:isar/isar.dart'; /// Isar 数据库提供者 final isarProvider = Provider((ref) { throw UnimplementedError('Isar instance must be provided'); }); /// 原理图仓库提供者 final schematicRepositoryProvider = Provider((ref) { final isar = ref.watch(isarProvider); return SchematicRepository(isar); }); /// 元件仓库提供者 final componentRepositoryProvider = Provider((ref) { final isar = ref.watch(isarProvider); return ComponentRepository(isar); }); /// 原理图仓库 class SchematicRepository { final Isar _isar; SchematicRepository(this._isar); Future> getAll() async { return await _isar.schematics.where().findAll(); } Future getById(int id) async { return await _isar.schematics.get(id); } Future save(Schematic schematic) async { await _isar.writeTxn(() async { await _isar.schematics.put(schematic); }); } Future delete(int id) async { await _isar.writeTxn(() async { await _isar.schematics.delete(id); }); } } /// 元件仓库 class ComponentRepository { final Isar _isar; ComponentRepository(this._isar); Future> getBySchematic(int schematicId) async { return await _isar.components .filter() .schematicIdEqualTo(schematicId) .findAll(); } Future save(Component component) async { await _isar.writeTxn(() async { await _isar.components.put(component); }); } Future delete(int id) async { await _isar.writeTxn(() async { await _isar.components.delete(id); }); } }