generateId function

String generateId({
  1. String? prefix,
  2. int length = 12,
})

Generate a short alphanumeric ID.

prefix is prepended with an underscore separator. length controls the random portion (default 12).

Implementation

String generateId({String? prefix, int length = 12}) {
  const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  final buf = StringBuffer();
  for (var i = 0; i < length; i++) {
    buf.write(chars[_secureRandom.nextInt(chars.length)]);
  }
  final id = buf.toString();
  return prefix != null ? '${prefix}_$id' : id;
}