generateRandomString function

String generateRandomString(
  1. int length
)

Implementation

String generateRandomString(int length) {
  const ch = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
  // Use a cryptographically secure RNG: this generator backs security-sensitive
  // values such as the PKCE code_verifier (RFC 7636) and the OAuth state token
  // (CSRF protection). The non-secure Random() is predictable (e.g. Math.random
  // on web) and must not be used for these.
  final r = Random.secure();
  return String.fromCharCodes(
    Iterable.generate(length, (_) => ch.codeUnitAt(r.nextInt(ch.length))),
  );
}