generateRandomString function

String generateRandomString(
  1. int length,
  2. StringType type
)

Implementation

String generateRandomString(int length, StringType type) {
  const String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const String numeric = '0123456789';
  const String symbols = '!@#\$%^&*()_+[]{}';
  const String alphaNumeric = alpha + numeric;
  const String alphaNumericSymbol = alpha + numeric + symbols;

  String chars = '';
  switch (type) {
    case StringType.alpha:
      chars = alpha;
      break;
    case StringType.numeric:
      chars = numeric;
      break;
    case StringType.alphaNumeric:
      chars = alphaNumeric;
      break;
    case StringType.symbol:
      chars = symbols;
      break;
    case StringType.alphaNumericSymbol:
      chars = alphaNumericSymbol;
      break;
  }

  Random rnd = Random();
  return String.fromCharCodes(
      Iterable.generate(length, (_) => chars.codeUnitAt(rnd.nextInt(chars.length))));
}