generateRandomString function

String generateRandomString(
  1. int length, {
  2. String chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
})

Function to generate a random string of specified length. Optionally allows specifying a custom set of characters to use.

Implementation

String generateRandomString(int length, {String chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'}) {
  Random rand = Random();
  List<String> charList = chars.split('');
  String result = '';

  for (int i = 0; i < length; i++) {
    result += charList[rand.nextInt(charList.length)];
  }

  return result;
}