Loading...
Loading...
Map backend API response types to Flutter Dart model correctly. Use when API returns string IDs or mismatched field names causing runtime errors.
npx skill4agent add hoangsoft90/ai_skills flutter-backend-api-mapping"1"int| Backend | Flutter Model | Fix |
|---|---|---|
| | |
| | |
| | Map field names explicitly |
| | Fallback |
factory Session.fromJson(Map<String, dynamic> json) {
// ID: SQLite trả String sometimes
final rawId = json['empId'] ?? json['id'] ?? 0;
final id = rawId is int
? rawId
: int.tryParse(rawId.toString()) ?? 0;
// Name: có fallback khi rỗng
final name = (json['empName'] ?? json['name'] ?? '') as String;
// Nested employee object
final emp = json['employee'] as Map<String, dynamic>?;
return Session(
id: id,
name: name.isEmpty ? 'Mới' : name,
initial: emp?['initial'] as String? ?? json['initial'] as String?,
colorIdx: emp?['color_idx'] as int? ?? json['color_idx'] as int?,
role: json['role'] as String? ?? 'employee',
);
}intStringempIdidempNamename