createCustomField method

Future<void> createCustomField(
  1. String fieldName,
  2. String label,
  3. FieldType fieldType, {
  4. PubliclyUpdatable isPubliclyUpdatable = PubliclyUpdatable.no,
  5. RequiredField isRequired = RequiredField.notRequired,
})

Implementation

Future<void> createCustomField(
  String fieldName,
  String label,
  FieldType fieldType, {
  PubliclyUpdatable isPubliclyUpdatable = PubliclyUpdatable.no,
  RequiredField isRequired = RequiredField.notRequired,
}) async {
  final url = '$mauticBaseUrl/api/fields/contact/new';
  final headers = getHeaders();

  final fieldData = {
    'label': label,
    'alias': fieldName,
    'type': fieldType,
    'is_publicly_updatable': isPubliclyUpdatable.value,
    'is_required': isRequired.value,
  };

  final response = await httpClient.post(
    Uri.parse(url),
    headers: headers,
    body: jsonEncode(fieldData),
  );

  if (response.statusCode != 200) {
    throw Exception('Failed to create custom field: ${response.statusCode}');
  }
}