From c2bf32af1c3982586e46085c06c29fac609e831a Mon Sep 17 00:00:00 2001 From: mohammad Date: Tue, 20 May 2025 14:53:28 +0300 Subject: [PATCH] Refactor device info model to handle optional fields more efficiently --- .../flush_sensor_bloc/flush_sensor_bloc.dart | 1 + .../devices/model/device_info_model.dart | 56 ++++++++++--------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/features/devices/bloc/flush_sensor_bloc/flush_sensor_bloc.dart b/lib/features/devices/bloc/flush_sensor_bloc/flush_sensor_bloc.dart index caaeb79..55b4d13 100644 --- a/lib/features/devices/bloc/flush_sensor_bloc/flush_sensor_bloc.dart +++ b/lib/features/devices/bloc/flush_sensor_bloc/flush_sensor_bloc.dart @@ -171,6 +171,7 @@ class FlushSensorBloc extends Bloc { deviceInfo = DeviceInfoModel.fromJson(response); deviceName = deviceInfo.name; emit(FlushSensorLoadingDeviceInfo(deviceInfo: deviceInfo)); + emit(FlushSensorUpdateState(flushSensorModel: deviceStatus)); } catch (e) { emit(FlushSensorFailedState(error: e.toString())); } diff --git a/lib/features/devices/model/device_info_model.dart b/lib/features/devices/model/device_info_model.dart index 583ef69..c1fd080 100644 --- a/lib/features/devices/model/device_info_model.dart +++ b/lib/features/devices/model/device_info_model.dart @@ -55,31 +55,33 @@ class DeviceInfoModel { factory DeviceInfoModel.fromJson(Map json) { return DeviceInfoModel( - activeTime: json['activeTime'], - category: json['category'], - categoryName: json['categoryName'], - createTime: json['createTime'], - gatewayId: json['gatewayId'], - icon: json['icon'], + activeTime: json['activeTime'] ?? '', + category: json['category'] ?? '', + categoryName: json['categoryName'] ?? '', + createTime: json['createTime'] ?? '', + gatewayId: json['gatewayId'] ?? '', + icon: json['icon'] ?? '', ip: json['ip'] ?? "", - lat: json['lat'], - localKey: json['localKey'], - lon: json['lon'], - model: json['model'], - name: json['name'], - nodeId: json['nodeId'], - online: json['online'], - ownerId: json['ownerId'], - productName: json['productName'], - sub: json['sub'], - timeZone: json['timeZone'], - updateTime: json['updateTime'], - uuid: json['uuid'], - productUuid: json['productUuid'], - productType: json['productType'], + lat: json['lat'] ?? '', + localKey: json['localKey'] ?? '', + lon: json['lon'] ?? '', + model: json['model'] ?? '', + name: json['name'] ?? '', + nodeId: json['nodeId'] ?? '', + online: json['online'] ?? '', + ownerId: json['ownerId'] ?? '', + productName: json['productName'] ?? '', + sub: json['sub'] ?? '', + timeZone: json['timeZone'] ?? '', + updateTime: json['updateTime'] ?? '', + uuid: json['uuid'] ?? '', + productUuid: json['productUuid'] ?? '', + productType: json['productType'] ?? '', permissionType: json['permissionType'] ?? '', - macAddress: json['macAddress'], - subspace: Subspace.fromJson(json['subspace']), + macAddress: json['macAddress'] ?? '', + subspace: json['subspace'] != null + ? Subspace.fromJson(json['subspace']) + : throw ArgumentError('subspace cannot be null'), ); } @@ -129,10 +131,10 @@ class Subspace { factory Subspace.fromJson(Map json) { return Subspace( - uuid: json['uuid'], - createdAt: json['createdAt'], - updatedAt: json['updatedAt'], - subspaceName: json['subspaceName'], + uuid: json['uuid'] ?? '', + createdAt: json['createdAt'] ?? '', + updatedAt: json['updatedAt'] ?? '', + subspaceName: json['subspaceName'] ?? '', ); }