randomPassword method

String randomPassword({
  1. bool letters = true,
  2. bool uppercase = false,
  3. bool numbers = false,
  4. bool specialChar = false,
  5. double passwordLength = 8,
})

get random password randomPassword

Implementation

String randomPassword(
    {bool letters = true,
    bool uppercase = false,
    bool numbers = false,
    bool specialChar = false,
    double passwordLength = 8}) {
  if (letters == false &&
      uppercase == false &&
      specialChar == false &&
      numbers == false) {
    letters = true;
  }
  String _lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";
  String _upperCaseLetters = _lowerCaseLetters.toUpperCase();
  String _numbers = "0123456789";
  String _special = "@#=+!£\$%&?[](){}";
  String _allowedChars = "";
  _allowedChars += (letters ? _lowerCaseLetters : '');
  _allowedChars += (uppercase ? _upperCaseLetters : '');
  _allowedChars += (numbers ? _numbers : '');
  _allowedChars += (specialChar ? _special : '');

  int i = 0;
  String _result = "";
  while (i < passwordLength.round()) {
    int randomInt = Random.secure().nextInt(_allowedChars.length);
    _result += _allowedChars[randomInt];
    i++;
  }

  /// return random password
  return _result;
}