genericSearchFunction function

Future<void> genericSearchFunction({
  1. required String searchParam,
  2. required ConceptClass conceptClass,
  3. required IGraphQlClient client,
  4. required StreamController streamController,
})

A generic search function for searching for, and listing problems, allergies medications, tests and diagnoses

It takes in a String searchParam, the conceptClass, the BuildContext context, and a stream controller streamController in which the data is added to

It then updates the stream controller with the returned data (if any) or an error if there was an error

Implementation

Future<void> genericSearchFunction({
  required String searchParam,
  required ConceptClass conceptClass,
  required IGraphQlClient client,
  required StreamController<dynamic> streamController,
}) async {
  // indicate processing ongoing on
  streamController.add(<String, dynamic>{'loading': true});

  final Map<String, dynamic> _variables =
      listConceptsQueryVariables(searchParam, conceptClass.name);

  // query for the results and check for errors
  final http.Response result =
      await client.query(listConceptsQuery, _variables);

  final Map<String, dynamic> payLoad = client.toMap(result);

  final String? error = client.parseError(payLoad);

  if (error != null) {
    await captureException(
      'Error while searching for patient',
      query: listConceptsQuery,
      error: error,
      response: payLoad,
    );
    return streamController.addError(<String, dynamic>{'error': error});
  }

  // add the returned data to the stream controller
  return payLoad['data'] != null
      ? streamController.add(payLoad['data'])
      : streamController.add(null);
}