toConstantName static method

String toConstantName(
  1. String assetName
)

Converts an asset name to a valid Dart constant name.

Applies camelCase convention and ensures the result is a valid Dart identifier by removing or replacing invalid characters.

Parameters:

  • assetName: The original asset name (usually filename without extension)

Returns a valid Dart constant name.

Implementation

static String toConstantName(String assetName) {
  // Remove file extension if present
  final nameWithoutExtension = assetName.contains('.')
      ? assetName.substring(0, assetName.lastIndexOf('.'))
      : assetName;

  // Apply camelCase transformation
  final camelCased = nameWithoutExtension.camelCase;

  // Ensure it starts with a letter or underscore
  if (RegExp(r'^[0-9]').hasMatch(camelCased)) {
    return '_$camelCased';
  }

  return camelCased;
}