CustomerAttributes.fromJson constructor

CustomerAttributes.fromJson(
  1. Map<String, dynamic> json
)

Create from JSON map.

This method extracts standard fields and treats any remaining top-level fields as additionalAttributes, similar to Android's MapToCustomerAtrributes method.

Implementation

factory CustomerAttributes.fromJson(Map<String, dynamic> json) {
  final tempJson = Map<String, dynamic>.from(json);

  // Extract standard fields
  final displayName = tempJson.remove('displayName') as String?;
  final firstName = tempJson.remove('firstName') as String?;
  final lastName = tempJson.remove('lastName') as String?;
  final email = tempJson.remove('email') as String?;
  final gender = tempJson.remove('gender') as String?;
  final mobile = tempJson.remove('mobile') as String?;
  final dateOfBirth = tempJson.remove('dateOfBirth') as String?;
  final joinDate = tempJson.remove('joinDate') as String?;
  final preferredLanguage = tempJson.remove('preferredLanguage') as String?;
  tempJson.remove('channel'); // Remove channel from JSON to prevent it from going into additionalAttributes

  // Extract custom attributes
  final customAttributes = (tempJson.remove('custom') as Map<String, dynamic>?)?.map(
    (k, e) => MapEntry(k, e as String),
  );

  // Any remaining fields become additionalAttributes
  final additionalAttributes = tempJson.isNotEmpty
      ? tempJson.map((k, e) => MapEntry(k, e.toString()))
      : null;

  return CustomerAttributes._(
    displayName: displayName,
    firstName: firstName,
    lastName: lastName,
    email: email,
    gender: gender,
    mobile: mobile,
    dateOfBirth: dateOfBirth,
    joinDate: joinDate,
    preferredLanguage: preferredLanguage,
    customAttributes: customAttributes,
    additionalAttributes: additionalAttributes,
  );
}