createRandomId static method

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}) {
  const characters =
      '0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  final characterRunes = characters.runes;
  const max = characters.length;
  final random = math.Random();
  final buffer = StringBuffer();
  for (var count = length; count > 0; count--) {
    final charIndex = random.nextInt(max);
    final rune = characterRunes.elementAt(charIndex);
    buffer.writeCharCode(rune);
  }

  return buffer.toString();
}