put method
Fetch the given resource from this bridge.
decrypter When the old tokens are read from local storage, they are
decrypted. This parameter allows you to provide your own decryption
method. This will be used in addition to the default decryption method.
This will be performed after the default decryption method.
If the PUT request is successful, then the original values in resource
will be refreshed and set to their current values. To disable this
behavior, set refreshOriginals to false.
Will return null if:
- The
resourcedoes not exist on this bridge - This bridge does not have an IP address
- This bridge does not have an application key
- The
resourcedoes not have any data to PUT - Any other unforeseen error
Implementation
Future<Map<String, dynamic>?> put(
Resource resource, {
String Function(String ciphertext)? decrypter,
bool refreshOriginals = true,
}) async {
if (ipAddress == null) return null;
if (applicationKey == null) return null;
final Map<String, dynamic> resourceJson =
resource.toJson(optimizeFor: OptimizeFor.put);
if (resourceJson.isEmpty) return null;
String body = JsonTool.writeJson(resourceJson);
try {
Map<String, dynamic>? result = await HueHttpRepo.put(
bridgeIpAddr: ipAddress!,
pathToResource: resource.id,
applicationKey: applicationKey!,
resourceType: resource.type,
body: body,
decrypter: decrypter,
);
if (result == null) return null;
if (result[ApiFields.errors] == null ||
(result[ApiFields.errors] as List<dynamic>).isEmpty) {
resource.refreshOriginals();
}
return result;
} catch (_) {
return null;
}
}