camelToSnake static method

String camelToSnake(
  1. String camelCase
)

Implementation

static String camelToSnake(String camelCase) {
// Use a regular expression to find uppercase letters and replace them with an underscore followed by the lowercase letter
String snakeCase = camelCase.replaceAllMapped(RegExp(r'[A-Z]'), (Match match) {
  return '_${match.group(0)!.toLowerCase()}';
});

// If the string starts with an underscore, remove it (in case the original string started with an uppercase letter)
if (snakeCase.startsWith('-')) {
  snakeCase = snakeCase.substring(1);
}

return snakeCase;
  }