getNotifications method Null safety
Retrieves Notifications from the backend. Optionally filters by
time window fromDate to endDate, whether the notification isRead
or if it is inCategory or withName. Pagination is supplied via
offset and limit.
Implementation
Future<BuiltList<Notification>> getNotifications({
final Instant? fromDate,
final Instant? endDate,
final bool? isRead,
final String? inCategory,
final String? withName,
final int offset = 0,
final int limit = 100,
}) async {
final filters = <String>[
if (fromDate != null)
'dateTime_gte: "${InstantPattern.general.format(fromDate)}"',
if (endDate != null)
'dateTime_lte: "${InstantPattern.general.format(endDate)}"',
if (isRead != null) 'read: ${isRead ? "true" : "false"}',
if (inCategory != null) 'category: "$inCategory"',
if (withName != null) 'withName: "$withName"',
];
final gqlQueryString = '''
query FetchNotifications {
notifications(sort: "dateTime:desc", start: $offset, limit: $limit, where: { ${filters.join(",")} }) {
${NotificationGqlEntity.gqlQueryFragment}
}
}
''';
try {
final queryResult = await graphQLClient.query<void>(QueryOptions(
document: gql(gqlQueryString), fetchPolicy: FetchPolicy.networkOnly));
if (queryResult.hasException) {
logger?.e(queryResult.exception.toString());
return BuiltList<Notification>();
}
final data = queryResult.data!['notifications'] as List<dynamic>;
final l = data
.map((dynamic e) =>
NotificationGqlEntity.fromJson(e as Map<String, dynamic>))
.where((element) => element != null)
.map((element) => element!.toNotification())
.toBuiltList();
if (!_notificationStreamController.isClosed) {
_notificationStreamController.add(l);
}
return l;
} catch (e, stackTrace) {
logger?.e('Could not read notifications', e.toString(), stackTrace);
return BuiltList<Notification>();
}
}