v4 method

String v4({
  1. Map<String, dynamic>? options,
})

v4() Generates a RNG version 4 UUID

By default it will generate a string based mathRNG, and will return a string. If you wish to use crypto-strong RNG, pass in UuidUtil.cryptoRNG

The first argument is an options map that takes various configuration options detailed in the readme.

http://tools.ietf.org/html/rfc4122.html#section-4.4

Implementation

String v4({Map<String, dynamic>? options}) {
  options ??= {};

  _initV4();
  // Use the built-in RNG or a custom provided RNG
  var positionalArgs =
      (options['positionalArgs'] != null) ? options['positionalArgs'] : [];
  var namedArgs = (options['namedArgs'] != null)
      ? options['namedArgs'] as Map<Symbol, dynamic>
      : const <Symbol, dynamic>{};
  // We cast to 'dynamic Function()' below instead of 'List<int> Function()'
  // as existing code may not return a closure of the correct type.
  var rng = (options['rng'] != null)
      ? Function.apply(options['rng'], positionalArgs, namedArgs) as List<int>
      : (_state['globalRNG']! as dynamic Function())() as List<int>;

  // Use provided values over RNG
  var rnds = options['random'] != null ? options['random'] as List<int> : rng;

  // per 4.4, set bits for version and clockSeq high and reserved
  rnds[6] = (rnds[6] & 0x0f) | 0x40;
  rnds[8] = (rnds[8] & 0x3f) | 0x80;

  return unparse(rnds);
}