find method
searches for a specific Resource
. That resource can be defined by
passing a full Resource
object, you may pass a resourceType
and id
or you can pass a search field
- since we are dealing with maps, this
should be a list of strings or integers, and this function will walk
through them:
field = 'name', 'given', 2
newValue = resource'name'
;
newValue = newValue'given'
;
newValue = newValue2
;
Implementation
Future<List<Resource>> find({
Resource? resource,
R4ResourceType? resourceType,
String? id,
List<Object>? field,
String? value,
String? pw,
}) async {
/// if we're just trying to match a resource
if (resource != null &&
resource.resourceType != null &&
(resource.fhirId != null || id != null)) {
final Resource? result = await get(
resourceType: resource.resourceType!,
id: resource.fhirId!,
pw: pw,
);
return result == null ? <Resource>[] : <Resource>[result];
} else if (resourceType != null && id != null) {
final Resource? result = await get(
resourceType: resourceType,
id: id,
pw: pw,
);
return result == null ? <Resource>[] : <Resource>[result];
} else if (resourceType != null && field != null && value != null) {
bool finder(Map<String, dynamic> finderResource) {
dynamic result = finderResource;
for (final Object key in field) {
result = result[key];
}
return result.toString() == value;
}
return (await search(resourceType: resourceType, finder: finder, pw: pw))
.toList();
} else {
throw const FormatException('Must have either: '
'\n1) a resource with a resourceType'
'\n2) a resourceType and an Id'
'\n3) a resourceType, a specific field, and the value of interest');
}
}