toCamelCaseAvoidKeywords function

String toCamelCaseAvoidKeywords(
  1. String input
)

Implementation

String toCamelCaseAvoidKeywords(String input) {
  // Replace any hyphens or underscores with spaces for easier processing
  input = input.replaceAll('-', ' ').replaceAll('_', ' ');

  // Split the string by spaces
  List<String> words = input.split(' ');

  // Convert the first word to lowercase and the rest to title case
  String camelCaseString = words.first.toLowerCase();

  for (int i = 1; i < words.length; i++) {
    if (words[i].isNotEmpty) {
      // Capitalize the first letter of the word and append it
      camelCaseString +=
          words[i][0].toUpperCase() + words[i].substring(1).toLowerCase();
    }
  }

  // Check if the camelCaseString is a Dart/Flutter keyword
  if (dartKeywords.contains(camelCaseString)) {
    camelCaseString += '_'; // Append an underscore to avoid keyword conflict
  }

  return camelCaseString;
}