createRandomId method Null safety

String createRandomId(
  1. {int length = 18}
)

Creates a new randomized ID text.

Specify length when a different length than 18 characters should be used. This can be used as a multipart boundary or a message-ID, for example.

Implementation

static String createRandomId({int length = 18}) {
  var characters =
      '0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var characterRunes = characters.runes;
  var max = characters.length;
  var random = math.Random();
  var buffer = StringBuffer();
  for (var count = length; count > 0; count--) {
    var charIndex = random.nextInt(max);
    var rune = characterRunes.elementAt(charIndex);
    buffer.writeCharCode(rune);
  }
  return buffer.toString();
}