import 'dart:convert'; class DoseRecord { final String id; final DateTime startTime; final DateTime endTime; final int flixotidePuffs; // 辅舒酮喷数 final int ventolinPuffs; // 万托林喷数 final bool rinsedMouth; // 是否漱口 final bool washedFace; // 是否洗脸 final bool isPrime; // 是否为试喷扣除 final String? note; DoseRecord({ required this.id, required this.startTime, required this.endTime, this.flixotidePuffs = 0, this.ventolinPuffs = 0, this.rinsedMouth = true, this.washedFace = true, this.isPrime = false, this.note, }); Duration get duration => endTime.difference(startTime); Map toMap() { return { 'id': id, 'startTime': startTime.toIso8601String(), 'endTime': endTime.toIso8601String(), 'flixotidePuffs': flixotidePuffs, 'ventolinPuffs': ventolinPuffs, 'rinsedMouth': rinsedMouth, 'washedFace': washedFace, 'isPrime': isPrime, 'note': note, }; } factory DoseRecord.fromMap(Map map) { return DoseRecord( id: map['id'] as String, startTime: DateTime.parse(map['startTime'] as String), endTime: DateTime.parse(map['endTime'] as String), flixotidePuffs: map['flixotidePuffs'] as int? ?? 0, ventolinPuffs: map['ventolinPuffs'] as int? ?? 0, rinsedMouth: map['rinsedMouth'] as bool? ?? true, washedFace: map['washedFace'] as bool? ?? true, isPrime: map['isPrime'] as bool? ?? false, note: map['note'] as String?, ); } String toJson() => json.encode(toMap()); factory DoseRecord.fromJson(String source) => DoseRecord.fromMap(json.decode(source) as Map); }