isValidIdentifier static method

bool isValidIdentifier(
  1. String name
)

Checks if a string is a valid Dart identifier.

  • Must start with a letter or underscore.
  • Must contain only letters, numbers, and underscores.
  • Must not be a Dart reserved keyword.

Implementation

static bool isValidIdentifier(String name) {
  if (name.isEmpty) return false;

  // Check regex: start with [a-zA-Z_], followed by [a-zA-Z0-9_]*
  final identifierRegex = RegExp(r'^[a-zA-Z_][a-zA-Z0-9_]*$');
  if (!identifierRegex.hasMatch(name)) return false;

  // Check reserved keywords
  return !_reservedKeywords.contains(name);
}