randomUsername static method

String randomUsername({
  1. int minLength = 6,
  2. int maxLength = 12,
})

Generate a random username.

Implementation

static String randomUsername({int minLength = 6, int maxLength = 12}) {
  const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  const numbers = '0123456789';
  const specialChars = '_-.';
  const allChars = letters + numbers + specialChars;

  // Random length between minLength and maxLength
  final length = _random.nextInt(maxLength - minLength + 1) + minLength;

  // Ensure the username starts with a letter
  final firstChar = letters[_random.nextInt(letters.length)];

  // Generate the rest of the username
  final restChars = List.generate(length - 1, (_) {
    return allChars[_random.nextInt(allChars.length)];
  }).join();

  return firstChar + restChars;
}