markAsRead method Null safety

Future<Notification> markAsRead(
  1. {required Notification notification}
)

Marks one Notification as read and publish the modified notification on the unread notifiations Stream

Implementation

Future<Notification> markAsRead(
    {required final Notification notification}) async {
  final gqlQueryString = '''
    mutation MarkNotificationsAsRead {
      updateNotification(input: { where: { id: "${notification.id}" }, data: { read: true } }) {
        notification {
          id
          read
        }
      }
    }
  ''';

  try {
    final queryResult = await graphQLClient.mutate<void>(MutationOptions(
      document: gql(gqlQueryString),
      fetchPolicy: FetchPolicy.networkOnly,
    ));

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return notification;
    }

    final data = queryResult.data!['updateNotification']['notification']
        as Map<String, dynamic>;
    final modifiedNotification =
        notification.rebuild((p0) => p0.read = data['read'] as bool);
    if (!_notificationStreamController.isClosed) {
      _notificationStreamController.add([modifiedNotification].toBuiltList());
    }
    return modifiedNotification;
  } catch (e, stackTrace) {
    logger?.e(
        'Could not mark notifications as read: ', e.toString(), stackTrace);
    return notification;
  }
}