如何关联Patients类与ListNote类,实现患者对应笔记加载功能
关联Patients与ListNote类实现患者笔记加载功能
这里提供两种常见的关联方案,根据你的数据管理场景选择即可:
方案1:在Patients类中直接持有笔记列表
适合内存中管理数据的场景,每个患者对象直接关联自己的笔记集合,获取和展示都最直接。
修改Patients类:
class Patients { final String name; final String firstname; final String dateofbirth; final String email; final String numero; final String id; final DateTime date; // 添加笔记列表属性,默认初始化为空列表 final List<ListNote> notes; Patients({ required this.name, required this.firstname, required this.dateofbirth, required this.email, required this.numero, required this.id, required this.date, List<ListNote>? notes, }) : notes = notes ?? []; }
使用方式
- 创建患者时直接传入对应笔记:
// 示例笔记 final initialNote = ListNote( title: "初诊记录", note: "患者主诉反复头痛3天", conclusion: "建议规律作息,3天后复诊" ); // 创建带笔记的患者 final patient = Patients( name: "Doe", firstname: "Jane", dateofbirth: "1985-05-15", email: "jane.doe@example.com", numero: "987654321", id: "P001", date: DateTime.now(), notes: [initialNote] );
- 点击患者进入详情页时,直接通过
patient.notes获取该患者的所有笔记进行展示。
方案2:通过患者ID关联笔记(适合持久化场景)
如果你的数据来自数据库或API,推荐给ListNote添加patientId字段,通过ID建立关联,这种结构更灵活,适合大规模数据管理。
修改ListNote类:
class ListNote { final String? title; final String? note; final String? conclusion; // 添加患者ID,关联对应的患者 final String patientId; ListNote({ this.title, this.note, this.conclusion, required this.patientId, }); }
使用方式
- 获取某患者的笔记时,通过患者ID过滤全局/数据源中的笔记列表:
// 假设存在全局或可访问的所有笔记集合 List<ListNote> allNotes = [ ListNote( title: "初诊记录", note: "头痛", conclusion: "休息观察", patientId: "P001" ), ListNote( title: "复诊记录", note: "症状缓解", conclusion: "无需用药", patientId: "P001" ) ]; // 根据患者ID筛选对应的笔记 List<ListNote> getPatientNotes(String patientId) { return allNotes.where((note) => note.patientId == patientId).toList(); }
- 点击患者进入详情页时,传递患者ID到详情页,调用上述方法获取对应笔记。
界面实现要点
患者列表页
遍历患者列表,可展示患者的笔记数量或预览:
ListView.builder( itemCount: patients.length, itemBuilder: (context, index) { final patient = patients[index]; return ListTile( title: Text("${patient.firstname} ${patient.name}"), // 方案1直接获取笔记数量 subtitle: Text("笔记数:${patient.notes.length}"), // 方案2需先查询:Text("笔记数:${getPatientNotes(patient.id).length}"), onTap: () { // 跳转到详情页,传递患者对象或ID Navigator.push( context, MaterialPageRoute( builder: (context) => PatientDetailPage(patient: patient), // 方案2传ID:PatientDetailPage(patientId: patient.id) ), ); }, ); }, );
患者详情页
接收患者数据,展示基本信息和关联笔记:
class PatientDetailPage extends StatelessWidget { final Patients patient; // 方案1使用 // final String patientId; // 方案2使用 const PatientDetailPage({super.key, required this.patient}); @override Widget build(BuildContext context) { // 方案1直接获取笔记 final notes = patient.notes; // 方案2调用方法:final notes = getPatientNotes(patientId); return Scaffold( appBar: AppBar(title: Text("${patient.firstname} ${patient.name}")), body: Column( children: [ // 患者基本信息展示 Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("出生日期:${patient.dateofbirth}"), Text("联系方式:${patient.numero}"), ], ), ), // 笔记列表展示 Expanded( child: ListView.builder( itemCount: notes.length, itemBuilder: (context, index) { final note = notes[index]; return Card( margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: Padding( padding: const EdgeInsets.all(12.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (note.title != null) Text(note.title!, style: const TextStyle(fontWeight: FontWeight.bold)), if (note.note != null) Padding( padding: const EdgeInsets.symmetric(vertical: 8), child: Text(note.note!), ), if (note.conclusion != null) Text("结论:${note.conclusion!}"), ], ), ), ); }, ), ), ], ), ); } }
内容的提问来源于stack exchange,提问作者Buubulle
相关产品推荐
相关产品推荐

