markAllAsRead method Null safety
Marks all the user Notification as read and publish clear update on
the unread notifications Stream. Returns true on success and false
on error.
Implementation
Future<bool> markAllAsRead() async {
const gqlQueryString = '''
mutation MarkNotificationsAsRead {
markAllNotificationsAsRead {
${NotificationGqlEntity.gqlQueryFragment}
}
}
''';
try {
final queryResult = await graphQLClient.mutate<void>(MutationOptions(
document: gql(gqlQueryString),
fetchPolicy: FetchPolicy.networkOnly,
));
if (queryResult.hasException) {
logger?.e(queryResult.exception.toString());
return false;
}
final data =
queryResult.data!['markAllNotificationsAsRead'] as List<dynamic>;
// translate the incoming JSON objects into calendar entries
final updatedNotifications = data
.map((dynamic untypedElement) =>
untypedElement as Map<String, dynamic>)
.map((jsonElement) => NotificationGqlEntity.fromJson(jsonElement))
.whereNotNull()
.map((gqlEntity) => gqlEntity.toNotification())
.toBuiltList();
if (!_notificationStreamController.isClosed) {
_notificationStreamController.add(updatedNotifications);
}
return true;
} catch (e, stackTrace) {
logger?.e(
'Could not mark notifications as read: ', e.toString(), stackTrace);
return false;
}
}