Skip to content

Commit a417279

Browse files
committed
fix(account): handle null case for notification in markAsRead
- Use firstWhereOrNull instead of firstWhere to safely handle null case - Add logging for attempted markAsRead on non-existent notification - Prevent exception by returning early if notification is not found
1 parent 24d15d1 commit a417279

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/account/bloc/in_app_notification_center_bloc.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:bloc/bloc.dart';
44
import 'package:core/core.dart';
5+
import 'package:collection/collection.dart';
56
import 'package:data_repository/data_repository.dart';
67
import 'package:equatable/equatable.dart';
78
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
@@ -117,11 +118,18 @@ class InAppNotificationCenterBloc
117118
InAppNotificationCenterMarkedAsRead event,
118119
Emitter<InAppNotificationCenterState> emit,
119120
) async {
120-
final notification = state.notifications.firstWhere(
121+
final notification = state.notifications.firstWhereOrNull(
121122
(n) => n.id == event.notificationId,
122-
orElse: () => throw Exception('Notification not found in state'),
123123
);
124124

125+
if (notification == null) {
126+
_logger.warning(
127+
'Attempted to mark a notification as read that does not exist in the '
128+
'current state: ${event.notificationId}',
129+
);
130+
return;
131+
}
132+
125133
// If already read, do nothing.
126134
if (notification.isRead) return;
127135

0 commit comments

Comments
 (0)