removePatient method Null safety

Future<Supporter?> removePatient(
  1. {required Patient patient}
)

Removes patient from the list of patients from the currently logged in supporter. Returns a new Supporter instance (and publishes it) if successful, otherwise returns null.

Implementation

Future<Supporter?> removePatient({
  required final Patient patient,
}) async {
  final gqlQueryString = '''
      mutation RemovePatientFromSupporter {
        removePatientFromSupporter(
          patient: ${patient.id}
        ) {
          ${SupporterGqlEntity.gqlQueryFragment}
        }
      }
    ''';

  try {
    final queryResult = await graphQLClient.mutate<void>(MutationOptions(
      document: gql(gqlQueryString),
      fetchPolicy: FetchPolicy.networkOnly,
    ));

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return null;
    }

    final data = queryResult.data!['removePatientFromSupporter']
        as Map<String, dynamic>;

    final supporterGqlFragment = SupporterGqlEntity.fromJson(data);

    if (supporterGqlFragment != null) {
      final updatedSupporter = supporterGqlFragment.toSupporter();
      _sendSupporterUpdate(supporter: updatedSupporter);
      return updatedSupporter;
    }
  } catch (e, stackTrace) {
    logger?.e(
        'Could not remove patient from supporter instance', e, stackTrace);
    return null;
  }

  return null;
}