getAll method Null safety

Future<BuiltSet<LocalizedType<Country>>> getAll()

Implementation

Future<BuiltSet<LocalizedType<Country>>> getAll() async {
  const gqlQueryString = '''
  query FetchAllCountriesWithLocalizations {
    countries {
      ${CountryGqlEntity.gqlQueryFragment}
    }
  }
  ''';

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

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return BuiltSet<LocalizedType<Country>>();
    }

    final data = queryResult.data!['countries'] as List<dynamic>;
    final l = data
        .map((dynamic e) =>
            CountryGqlEntity.fromJson(e as Map<String, dynamic>))
        .where((element) => element != null)
        .map((e) => e!.toLocalizedTypeCountry());

    return l.toBuiltSet();
  } catch (e, stackTrace) {
    logger?.e('Could not read list of countries', e.toString(), stackTrace);
    return BuiltSet<LocalizedType<Country>>();
  }
}