Search packages, repositories and app categories

Package detailsv0.1.2

shortid

ShortId creates short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids etc. (port from js)

AndroidiOSWebmacOSWindowsLinux
30-day downloads
240K
Likes
21
Pub points
150

Install package

flutter pub add shortid

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

ShortId generates short, non-sequential, URL-friendly unique identifiers ideal for use in URL shorteners, databases like MongoDB and Redis, and other systems requiring compact, secure IDs. Designed as a Dart port of the popular JavaScript library, it ensures uniqueness and avoids predictable patterns. The package is lightweight and compatible across all major platforms, making it suitable for cross-platform applications needing reliable ID generation without relying on sequential or long identifiers.

Use cases

  • URL shortening services
  • Database primary keys
  • Session token generation
  • Caching key creation
  • Secure identifier generation

Key features

  • Generates short, random IDs
  • URL-safe characters
  • Non-sequential and unpredictable
  • Cross-platform support
  • Lightweight and fast
  • Dart port of JavaScript library

Suitable for

  • Web and mobile apps needing compact IDs
  • Backend services with database integration
  • Applications requiring secure, non-guessable identifiers
  • Projects using MongoDB or Redis
  • Developers familiar with the JavaScript ShortId

Considerations

  • Not cryptographically secure for high-security use cases
  • IDs are not guaranteed to be globally unique without additional checks
  • Depends on random number generation quality
  • Limited to 16-character default length
  • No built-in collision detection

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.

  • By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _-
  • Supports custom seeds, custom alphabet.
  • Can generate any number of ids without duplicates, even millions per day.
  • Good replacement for Mongo ID/Mongoose ID.
  • Includes tests.

ShortId does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.

Usage
import 'package:shortid/shortid.dart';

print(shortid.generate());
// PPBqWA9

shortid.generate()

Returns string non-sequential unique id.

Example

users.insert({
  _id: shortid.generate(),
  name: '...',
  email: '...'
});

shortid.characters(string)

Default: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'

Returns new alphabet as a string

Recommendation: If you don't like _ or -, you can to set new characters to use.

Optional

Change the characters used.

You must provide a string of all 64 unique characters. Order is not important.

The default characters provided were selected because they are url safe.

Example

// use $ and @ instead of - and _
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
// any 64 unicode characters work, but I wouldn't recommend this.
shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');

shortid.seed(integer)

Default: 1

Recommendation: You typically won't want to change this.

Optional

Choose a unique value that will seed the random number generator so users won't be able to figure out the pattern of the unique ids. Call it just once in your application before using shortId and always use the same value in your application.

Most developers won't need to use this, it's mainly for testing ShortId.

If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.

Example

shortid.seed(1000);

Categories

Source repository

Projects using this package