format method
Returns the address formatted as a single, human-readable line.
The method builds a concise, display-friendly address by selecting the relevant address components, trimming whitespace, omitting null or empty values, collapsing adjacent duplicates, and joining the remaining parts with a readable separator (", "). The ordering of components follows a common mailing/display convention.
Parameters
excludeFields(optional): A list of address field identifiers to omit from the formatted output. Null or an empty list means "do not exclude any fields".includeFields(optional): A list of address field identifiers to include in the output. When provided, only the fields in this list are considered;excludeFieldsis ignored. An empty list will result in an empty string.
Returns
String: The formatted address.
Implementation
String format({
final List<AddressField>? excludeFields,
final List<AddressField>? includeFields,
}) {
final StringBuffer buffer = StringBuffer();
final BitField<AddressField> bitfield = BitField<AddressField>(
AddressField.addrLast.id + 1,
);
for (final AddressField field in includeFields ?? AddressField.values) {
bitfield[field] = true;
}
for (final AddressField field in excludeFields ?? <AddressField>[]) {
bitfield[field] = false;
}
for (final AddressField field in AddressField.values) {
if (!bitfield[field]) {
continue;
}
final String? value = getField(field);
String? prevValue;
if (field.id > 0) {
prevValue = getField(AddressFieldExtension.fromId(field.id - 1));
}
if (value != null && value.isNotEmpty && value != prevValue) {
buffer.write(value);
buffer.write(', ');
}
}
final String result = buffer.toString();
if (result.endsWith(', ')) {
return result.substring(0, result.length - 2);
} else {
return result;
}
}