getContentElements method Null safety

Future<BuiltList<ContentElement>> getContentElements(
  1. {required int offset,
  2. required int limit,
  3. required LocalizedType<Language>? language,
  4. required ContentElementType type,
  5. BuiltSet<User>? likedByUsers,
  6. required BuiltSet<LocalizedType<Interest>> applicableInterests,
  7. required BuiltSet<LocalizedType<Condition>> applicableConditions}
)

Retrieves content elements for a user. Same behaviour as getRelevantContent but without random seed, always returns the same order. The parameter getOnlyFavorites indicates if only should return content elements liked by the user

Implementation

Future<BuiltList<ContentElement>> getContentElements({
  required final int offset,
  required final int limit,
  required final LocalizedType<Language>? language,
  required final ContentElementType type,
  BuiltSet<User>? likedByUsers,
  required final BuiltSet<LocalizedType<Interest>> applicableInterests,
  required final BuiltSet<LocalizedType<Condition>> applicableConditions,
}) async {
  final requestedLocale = language?.defaultValue.isoCode ?? 'en';

  likedByUsers ??= BuiltSet<User>();
  final gqlQueryString = '''
  query FetchContent {
    contentElements(
      start: $offset,
      limit: $limit,
      where: {
        ${likedByUsers.isNotEmpty ? 'likedBy_contains: [${likedByUsers.map((e) => e.userId).join(',')}], ' : ''},
        ${applicableInterests.isNotEmpty ? 'applicableInterests: [${applicableInterests.map((e) => e.value(locale: requestedLocale).id).join(',')}], ' : ''},
        ${applicableConditions.isNotEmpty ? 'applicableConditions: [${applicableConditions.map((e) => e.value(locale: requestedLocale).id).join(',')}], ' : ''},
        type: "${type.name}",
        locale: "$requestedLocale"
      }
    ) {
      ${ContentElementGqlEntity.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<ContentElement>();
    }

    final data = queryResult.data!['contentElements'] as List<dynamic>;
    final l = data
        .map((dynamic e) =>
            ContentElementGqlEntity.fromJson(e as Map<String, dynamic>))
        .where((element) => element != null)
        .map((element) => element!.toContentElement())
        .toBuiltList();

    if (!_contentElementStreamController.isClosed) {
      _contentElementStreamController
          .add(l.map((c) => EntityRead<ContentElement>(c)).toBuiltList());
    }

    return l;
  } catch (e, stackTrace) {
    logger?.e('Could not read content elements', e.toString(), stackTrace);
    return BuiltList<ContentElement>();
  }
}