getContentElements method Null safety
- {required int offset,
- required int limit,
- required LocalizedType<
Language> ? language, - required ContentElementType type,
- BuiltSet<
User> ? likedByUsers, - required BuiltSet<
LocalizedType< applicableInterests,Interest> > - required BuiltSet<
LocalizedType< applicableConditions}Condition> >
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>();
}
}