attributes property

Future<Map<String, BatchUserAttribute>> get attributes

Read the saved attributes. Reading is asynchronous so as not to interfere with saving operations.

Implementation

Future<Map<String, BatchUserAttribute>> get attributes async {
  Map<String, Map<dynamic, dynamic>>? rawAttributes =
      await _channel.invokeMapMethod("user.fetch.attributes");

  if (rawAttributes == null) {
    throw BatchUserInternalError(code: 1);
  }

  Map<String, BatchUserAttribute> attributes = {};
  rawAttributes.forEach((key, rawTypedValue) {
    dynamic castedValue;
    BatchUserAttributeType type;
    dynamic rawValue = rawTypedValue["value"];

    if (rawValue == null) {
      throw BatchUserInternalError(code: 2);
    }

    String? rawType = rawTypedValue["type"];
    switch (rawType) {
      case "d":
        type = BatchUserAttributeType.date;
        int rawDate = rawValue as int;
        castedValue =
            DateTime.fromMillisecondsSinceEpoch(rawDate, isUtc: true);
        break;
      case "i":
        type = BatchUserAttributeType.integer;
        castedValue = rawValue as int;
        break;
      case "f":
        type = BatchUserAttributeType.double;
        castedValue = rawValue as double;
        break;
      case "b":
        type = BatchUserAttributeType.boolean;
        castedValue = rawValue as bool;
        break;
      case "s":
        type = BatchUserAttributeType.string;
        castedValue = rawValue as String;
        break;
      case "u":
        type = BatchUserAttributeType.url;
        castedValue = Uri.parse(rawValue as String);
        break;
      default:
        throw BatchUserInternalError(code: 3);
    }

    attributes[key] = BatchUserAttribute(type: type, value: castedValue);
  });
  return attributes;
}