post method

Future<Map<String, dynamic>?> post(
  1. Resource resource, [
  2. bool refreshOriginals = true
])

Fetch the given resource from this bridge.

If the POST 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 resource does not exist on this bridge
  • This bridge does not have an IP address
  • This bridge does not have an application key
  • The resource does not have any data to POST
  • Any other unforeseen error

Implementation

Future<Map<String, dynamic>?> post(Resource resource,
    [bool refreshOriginals = true]) async {
  if (ipAddress == null) return null;
  if (applicationKey == null) return null;

  final Map<String, dynamic> resourceJson =
      resource.toJson(optimizeFor: OptimizeFor.post);

  if (resourceJson.isEmpty) return null;

  String body = JsonTool.writeJson(resourceJson);

  try {
    Map<String, dynamic>? result = await HueHttpRepo.post(
      bridgeIpAddr: ipAddress!,
      pathToResource: resource.id,
      applicationKey: applicationKey!,
      resourceType: resource.type,
      body: body,
    );

    if (result == null) return null;

    if (result[ApiFields.errors] == null ||
        (result[ApiFields.errors] as List<dynamic>).isEmpty) {
      resource.refreshOriginals();
    }

    return result;
  } catch (_) {
    return null;
  }
}