isValidDartClassName function

bool isValidDartClassName(
  1. String text
)

Validates whether a string can be converted to a valid Dart class name.

Checks if the string, when converted to PascalCase, follows Dart's class naming conventions:

  • Must start with an uppercase letter
  • Can only contain letters and numbers
  • No special characters or underscores

Examples:

isValidDartClassName('user_profile') // Returns true ('UserProfile')
isValidDartClassName('first_name') // Returns true ('FirstName')
isValidDartClassName('123_invalid') // Returns false (starts with number)
isValidDartClassName('user-profile') // Returns false (contains hyphen)

text The string to validate (typically a database table name) Returns true if the string can become a valid Dart class name

Implementation

bool isValidDartClassName(String text) {
  final validClassNameRegExp = RegExp(r'^[A-Z][A-Za-z0-9]*$');
  final snakeText = snakeToPascalCase(text);
  return validClassNameRegExp.hasMatch(snakeText);
}