getPendingCalendarEntries method Null safety

Future<BuiltList<CalendarEntry>> getPendingCalendarEntries(
  1. {required String userId}
)

Get all calendar entries when in its invitation statuses for current user is sent or if is accepted but the other participant cancelled it. In resume, all invitations which need an action from the current user

Implementation

Future<BuiltList<CalendarEntry>> getPendingCalendarEntries({
  required final String userId,
}) async {
  const gqlQueryString = '''
    query FetchPendingInvitations {
      pendingInvitationsForUser {
        ${InvitationGqlEntity.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<CalendarEntry>();
    }

    final data =
        queryResult.data!['pendingInvitationsForUser'] as List<dynamic>;

    // translate the incoming JSON objects into calendar entries
    final calendarEntries = data
        .map((dynamic untypedElement) =>
            untypedElement as Map<String, dynamic>)
        .map((jsonElement) => CalendarEntryGqlEntity.fromJson(
            jsonElement['calendarEntry'] as Map<String, dynamic>))
        .whereNotNull()
        .map((gqlEntity) => gqlEntity.toCalendarEntry())
        .toBuiltList();

    // NOTE: This piece of code is only a temporary thing. Right now the
    // app expects the invitation message in the description field of the
    // calendar entry, even though the description field has a different
    // purpose. The lines below move the invitation message into the
    // calendar entry's description field. When the app is changed, the line
    // below needs to be removed.
    final updatedEntries = calendarEntries
        .map((entry) => entry.rebuild((builder) => builder
          ..description = entry.invitations
              .firstWhere(
                  (invitation) => invitation.participant.userId == userId)
              .message))
        .toBuiltList();

    if (!_calendarStreamController.isClosed) {
      _calendarStreamController
          .add(updatedEntries.map((p0) => EntityRead(p0)).toBuiltList());
    }

    return updatedEntries;
  } catch (e, stackTrace) {
    logger?.e('Could not read pending invitations', e.toString(), stackTrace);
    return BuiltList<CalendarEntry>();
  }
}