getNeeds method Null safety

Future<BuiltList<Need>> getNeeds(
  1. {required Instant fromDate,
  2. required Instant endDate}
)

Retrieve all needs entries between two dates. Note that the returned Need has the invitation for all participants of the event only when the originator User is the same of the User which does the query. If is a different User, only will return the NeedInvitation of the originator User and the User which does the query. states==null || states.len==0, will retrieve all needs entries independing of the invitation status of the user states!=null && states.len!=0, only will retrieve needs which their invitation status for the user match with any of the target statuses

Implementation

Future<BuiltList<Need>> getNeeds({
  required final Instant fromDate,
  required final Instant endDate,
}) async {
  final gqlQueryString = '''
    query FetchAllNeedInvitations {
      existingNeedInvitationsForUser(fromDate: "${fromDate.toString()}", endDate: "${endDate.toString()}") {
        ${NeedInvitationGqlEntity.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<Need>();
    }

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

    // translate the incoming JSON objects into needs
    final needEntries = data
        .map((dynamic untypedElement) =>
            untypedElement as Map<String, dynamic>)
        .map((jsonElement) => NeedGqlEntity.fromJson(
            jsonElement['need'] as Map<String, dynamic>))
        .whereNotNull()
        .map((gqlEntity) => gqlEntity.toNeed())
        .toBuiltList();

    if (!_needsSubject.isClosed) {
      _needsSubject.add(
        needEntries.map((p0) => EntityRead(p0)).toBuiltList(),
      );
    }
    return needEntries;
  } catch (e, stackTrace) {
    logger?.e('Could not read needs', e.toString(), stackTrace);
    return BuiltList<Need>();
  }
}