newUlid function
A ULID: 10 Crockford base32 characters of epoch milliseconds, then 16 of
randomness (80 bits). now defaults to the clock and random to
Random.secure(); both are for tests. Throws RangeError for a time
before 1970 or past the ULID range, so the first character is always
0–7.
Implementation
String newUlid({DateTime? now, Random? random}) {
var time = (now ?? DateTime.now()).millisecondsSinceEpoch;
RangeError.checkValueInInterval(time, 0, _maxUlidMillis, 'now');
final source = random ?? _secure;
final out = StringBuffer();
final ts = List.filled(10, '');
for (var i = 9; i >= 0; i--) {
ts[i] = _crockford[time % 32];
time ~/= 32;
}
out.writeAll(ts);
for (var i = 0; i < 16; i++) {
out.write(_crockford[source.nextInt(32)]);
}
return out.toString();
}