toLabel method
Converts a field name to a human-readable label Examples:
- 'testField' => 'Test Field'
- 'test_field' => 'Test Field'
- 'TestField' => 'Test Field'
Implementation
String toLabel() {
if (isEmpty) return this;
// Insert space before capital letters (except at the start)
String result = replaceAllMapped(
RegExp(r'(?<!^)([A-Z])'),
(match) => ' ${match.group(1)}',
);
// Replace underscores with spaces
result = result.replaceAll('_', ' ');
// Capitalize the first character
result = result[0].toUpperCase() + result.substring(1);
// Clean up multiple spaces and trim
result = result.replaceAll(RegExp(r'\s+'), ' ').trim();
return result;
}