getCalendarEntry method Null safety

Future<CalendarEntry?> getCalendarEntry(
  1. {required String id}
)

Retrieves a single calendar entry by id. Returns null if calendar entry is not found.

Implementation

Future<CalendarEntry?> getCalendarEntry({required String id}) async {
  final gqlQueryString = '''
    query FetchCalendarEntry {
      calendarEntry(id: "$id") {
        ${CalendarEntryGqlEntity.gqlQueryFragment}
      }
    }
  ''';

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

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

    final data = queryResult.data!['calendarEntry'] as Map<String, dynamic>;

    // translate the incoming JSON objects into calendar entries
    final calendarGqlEntry = CalendarEntryGqlEntity.fromJson(data);

    if (calendarGqlEntry == null) {
      logger?.e("Could not deserialize calendar entry");
      return null;
    }

    final updatedCalendarEntry = calendarGqlEntry.toCalendarEntry();
    if (!_calendarStreamController.isClosed) {
      _calendarStreamController
          .add([EntityRead(updatedCalendarEntry)].toBuiltList());
    }
    return updatedCalendarEntry;
  } catch (e, stackTrace) {
    logger?.e(
        'Could not find calendar entry by id: ', e.toString(), stackTrace);
    return null;
  }
}