From 5ff5f65549ac7bdf333156b5c2364feb04f80629 Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Wed, 3 Apr 2024 02:12:35 +0300 Subject: [PATCH] Refactor emit calls to use emitSafe method Replace direct calls to emit with emitSafe method to ensure emitting states only when the cubit is not closed. This helps prevent errors when emitting states after the cubit is closed. --- lib/features/devices/bloc/devices_cubit.dart | 69 +++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 9432f11..7361415 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -39,11 +39,18 @@ class DevicesCubit extends Cubit { // return _instance!; // } - // @override - // Future close() { - // _isClosed = true; - // return super.close(); - // } + @override + Future close() { + _isClosed = true; + return super.close(); + } + + void emitSafe(DevicesState newState) { + final cubit = this; + if (!cubit.isClosed) { + cubit.emit(newState); + } + } static DevicesCubit get(context) => BlocProvider.of(context); @@ -57,14 +64,14 @@ class DevicesCubit extends Cubit { allCategories![i].isSelected = false; } } - emit(DevicesCategoryChanged()); + emitSafe(DevicesCategoryChanged()); } unselectAllCategories() { for (var category in allCategories!) { category.isSelected = false; } - emit(DevicesCategoryChanged()); + emitSafe(DevicesCategoryChanged()); } Widget? get chosenCategoryView { @@ -108,14 +115,14 @@ class DevicesCubit extends Cubit { for (var device in category.devices!) { if (device.isSelected) { category.isSelected = false; - emit(DeviceSelected()); + emitSafe(DeviceSelected()); return; } } } } device.isSelected = !device.isSelected; - emit(DeviceSelected()); + emitSafe(DeviceSelected()); } DeviceModel? getSelectedDevice() { @@ -149,7 +156,7 @@ class DevicesCubit extends Cubit { } updateDevicesStatus(category); - emit(CategorySwitchChanged()); + emitSafe(CategorySwitchChanged()); } turnOnOffDevice(DeviceModel device) { @@ -162,7 +169,7 @@ class DevicesCubit extends Cubit { } }); updateDevicesStatus(category); - emit(DeviceSwitchChanged()); + emitSafe(DeviceSwitchChanged()); } updateDevicesStatus(DevicesCategoryModel category) { @@ -173,15 +180,15 @@ class DevicesCubit extends Cubit { //check if there any ac have a different status than the initial ==> turn off the universal switch if (ac.isOnline != tempStatus) { category.devicesStatus = null; - emit(DeviceSwitchChanged()); + emitSafe(DeviceSwitchChanged()); return; } category.devicesStatus = tempStatus; - emit(DeviceSwitchChanged()); + emitSafe(DeviceSwitchChanged()); } } else { category.devicesStatus = null; - emit(DeviceSwitchChanged()); + emitSafe(DeviceSwitchChanged()); } } } @@ -194,7 +201,7 @@ class DevicesCubit extends Cubit { } changeCategorySwitchValue(category); updateDevicesStatus(category); - emit(CategorySwitchChanged()); + emitSafe(CategorySwitchChanged()); } } } @@ -207,7 +214,7 @@ class DevicesCubit extends Cubit { } changeCategorySwitchValue(category); updateDevicesStatus(category); - emit(CategorySwitchChanged()); + emitSafe(CategorySwitchChanged()); } } } @@ -217,7 +224,7 @@ class DevicesCubit extends Cubit { for (var device in category.devices!) { if (device.isOnline ?? false) { category.devicesStatus = false; - emit(CategorySwitchChanged()); + emitSafe(CategorySwitchChanged()); return; } } @@ -235,16 +242,16 @@ class DevicesCubit extends Cubit { } Navigator.popUntil(context, (route) => route.isFirst); - emit(DevicesCategoryChanged()); + emitSafe(DevicesCategoryChanged()); } deviceControl(DeviceControlModel control) async { - emit(DeviceControlLoading()); + emitSafe(DeviceControlLoading()); try { await DevicesAPI.controlDevice(control); - emit(DeviceControlSuccess()); + emitSafe(DeviceControlSuccess()); } on DioException catch (e) { - emit(DeviceControlError(ServerFailure.fromDioError(e).errMessage)); + emitSafe(DeviceControlError(ServerFailure.fromDioError(e).errMessage)); } } @@ -252,11 +259,11 @@ class DevicesCubit extends Cubit { if (_isClosed) return; try { - emit(DevicesCategoriesLoading()); + emitSafe(DevicesCategoriesLoading()); allCategories = await DevicesAPI.fetchGroups(spaceId); - emit(DevicesCategoriesSuccess()); + emitSafe(DevicesCategoriesSuccess()); } on DioException catch (error) { - emit( + emitSafe( DevicesCategoriesError(ServerFailure.fromDioError(error).errMessage), ); } @@ -266,7 +273,7 @@ class DevicesCubit extends Cubit { if (_isClosed) return; try { - emit(GetDevicesLoading()); + emitSafe(GetDevicesLoading()); int roomIndex = HomeCubit.getInstance() .selectedSpace! .rooms! @@ -274,9 +281,9 @@ class DevicesCubit extends Cubit { HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices = await SpacesAPI.getDevicesByRoomId(roomId); - emit(GetDevicesSuccess()); + emitSafe(GetDevicesSuccess()); } on DioException catch (error) { - emit( + emitSafe( GetDevicesError(ServerFailure.fromDioError(error).errMessage), ); } @@ -303,17 +310,17 @@ class DevicesCubit extends Cubit { // setLightingMode(DeviceModel light, LightMode mode) { // light.lightingMode = // lightModes.entries.firstWhere((element) => element.value == mode).key; -// emit(LightModeChanged(mode)); +// emitSafe(LightModeChanged(mode)); // } // // toggleLight(DeviceModel light) { // light.isOnline != null ? light.isOnline = !light.isOnline! : light.isOnline = true; -// emit(LightToggled(light)); +// emitSafe(LightToggled(light)); // } // // setColor(DeviceModel light, int color) { // light.color = color; -// emit(LightColorChanged(color)); +// emitSafe(LightColorChanged(color)); // } // // int getBrightness(DeviceModel light) { @@ -324,7 +331,7 @@ class DevicesCubit extends Cubit { // value = (value / 5).ceil() * 5; // if (value != light.brightness) { // light.brightness = value; -// emit(LightBrightnessChanged(value)); +// emitSafe(LightBrightnessChanged(value)); // } // } }