From e1a24651307b67943c2ac4df6e051953acdcb735 Mon Sep 17 00:00:00 2001 From: Faris Armoush Date: Wed, 23 Apr 2025 13:11:25 +0300 Subject: [PATCH] Fix deviceId assignment in FlushMountedPresenceSensorBlocFactory and update _listenToChanges method for async handling --- .../flush_mounted_presence_sensor_bloc.dart | 30 +++++++++++-------- ..._mounted_presence_sensor_bloc_factory.dart | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart index 9b62cb4e..0bc15cd2 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:developer'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; @@ -80,28 +81,31 @@ class FlushMountedPresenceSensorBloc } } - void _listenToChanges( + Future _listenToChanges( Emitter emit, String deviceId, - ) { - try { - final ref = FirebaseDatabase.instance.ref( - 'device-status/$deviceId', - ); + ) async { + final ref = FirebaseDatabase.instance.ref( + 'device-status/$deviceId', + ); - ref.onValue.listen((DatabaseEvent event) { + await ref.onValue.listen( + (DatabaseEvent event) async { Map usersMap = event.snapshot.value as Map; List statusList = []; - - usersMap['status'].forEach((element) { + + (usersMap['status'] as List?)?.forEach((element) { statusList.add(Status(code: element['code'], value: element['value'])); }); - + deviceStatus = FlushMountedPresenceSensorModel.fromJson(statusList); - emit(FlushMountedPresenceSensorLoadingNewSate(model: deviceStatus)); - }); - } catch (_) {} + if (!emit.isDone) { + emit(FlushMountedPresenceSensorLoadingNewSate(model: deviceStatus)); + } + }, + onError: (error) => log(error.toString(), name: 'FirebaseDatabaseError'), + ).asFuture(); } void _onFlushMountedPresenceSensorChangeValueEvent( diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart index 537189af..f1342eec 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart @@ -9,7 +9,7 @@ abstract final class FlushMountedPresenceSensorBlocFactory { required String deviceId, }) { return FlushMountedPresenceSensorBloc( - deviceId: '', + deviceId: deviceId, controlDeviceService: DebouncedControlDeviceService( decoratee: RemoteControlDeviceService(), ),