ChatDirectLinkSlug.generate constructor

ChatDirectLinkSlug.generate([
  1. int length = 10
])

Creates a random ChatDirectLinkSlug of the provided length.

Implementation

factory ChatDirectLinkSlug.generate([int length = 10]) {
  final Random r = Random();
  const String chars =
      'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890_-';

  return ChatDirectLinkSlug(
    List.generate(length, (i) {
      // `-` and `_` being the last might not be parsed as a link by some
      // applications.
      if (i == length - 1) {
        final str = chars.replaceFirst('-', '').replaceFirst('_', '');
        return str[r.nextInt(str.length)];
      }

      return chars[r.nextInt(chars.length)];
    }).join(),
  );
}