isValidKey static method

bool isValidKey(
  1. String? s
)

Implementation

static bool isValidKey(String? s) {
  // Check if the string is null or empty
  if (s == null || s.isEmpty) {
    return false;
  }

  // Check if the first character is valid: must be alphabetic or an underscore
  final first = s[0];
  if (!RegExp(r'[A-Za-z_]').hasMatch(first)) {
    return false;
  }

  // Check each subsequent character
  for (int i = 1; i < s.length; i++) {
    final c = s[i];
    if (!RegExp(r'[A-Za-z0-9_.]').hasMatch(c)) {
      return false;
    }
  }

  // All characters are valid
  return true;
}