getRandomString method

String getRandomString({
  1. int lowersCount = 3,
  2. int uppersCount = 3,
  3. int numbersCount = 3,
  4. int specialsCount = 1,
  5. String specials = '_',
  6. bool canSpecialRepeat = true,
})

Implementation

String getRandomString({
  int lowersCount = 3,
  int uppersCount = 3,
  int numbersCount = 3,
  int specialsCount = 1,
  String specials = '_',
  bool canSpecialRepeat = true,
}) {
  var uuid = '';

  for (var i = 0; i < lowersCount; i++) {
    uuid += lowers[_random.nextInt(lowers.length)];
  }
  for (var i = 0; i < uppersCount; i++) {
    uuid += uppers[_random.nextInt(uppers.length)];
  }
  for (var i = 0; i < numbersCount; i++) {
    uuid += numbers[_random.nextInt(numbers.length)];
  }

  if (!canSpecialRepeat) {
    if (specialsCount > specials.length) {
      specialsCount = specials.length;
    }
  }

  for (var i = 0; i < specialsCount; i++) {
    var specialChar = specials[_random.nextInt(specials.length)];
    uuid += specialChar;
    if (!canSpecialRepeat) {
      specials = specials.replaceAll(specialChar, '');
    }
  }
  var shuffled = (uuid.split('')..shuffle()).join();
  return shuffled != '' ? shuffled : 'yoo';
}