mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
Refactor GatewayModel.fromJson to use a loop for status parsing instead of firstWhere.
This commit is contained in:
@ -14,27 +14,36 @@ class GatewayModel {
|
||||
});
|
||||
|
||||
factory GatewayModel.fromJson(Map<String, dynamic> json) {
|
||||
final status = json['status'] as List<dynamic>;
|
||||
final status = json['status'] as List<dynamic>? ?? <dynamic>[];
|
||||
|
||||
final switchAlarmSound = status.firstWhere(
|
||||
(item) => item['code'] == 'switch_alarm_sound',
|
||||
)['value'] as bool;
|
||||
final masterState = status.firstWhere(
|
||||
(item) => item['code'] == 'master_state',
|
||||
)['value'] as String;
|
||||
final factoryReset = status.firstWhere(
|
||||
(item) => item['code'] == 'factory_reset',
|
||||
)['value'] as bool;
|
||||
final alarmActive = status.firstWhere(
|
||||
(item) => item['code'] == 'alarm_active',
|
||||
)['value'] as String;
|
||||
bool? switchAlarmSound;
|
||||
String? masterState;
|
||||
bool? factoryReset;
|
||||
String? alarmActive;
|
||||
|
||||
for (final item in status) {
|
||||
switch (item['code']) {
|
||||
case 'switch_alarm_sound':
|
||||
switchAlarmSound = item['value'] as bool;
|
||||
break;
|
||||
case 'master_state':
|
||||
masterState = item['value'] as String;
|
||||
break;
|
||||
case 'factory_reset':
|
||||
factoryReset = item['value'] as bool;
|
||||
break;
|
||||
case 'alarm_active':
|
||||
alarmActive = item['value'] as String;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return GatewayModel(
|
||||
uuid: json['uuid'] as String,
|
||||
switchAlarmSound: switchAlarmSound,
|
||||
masterState: masterState,
|
||||
factoryReset: factoryReset,
|
||||
alarmActive: alarmActive,
|
||||
switchAlarmSound: switchAlarmSound ?? false,
|
||||
masterState: masterState ?? '',
|
||||
factoryReset: factoryReset ?? false,
|
||||
alarmActive: alarmActive ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user