generateUUIDv4 static method

String generateUUIDv4()

Implementation

static String generateUUIDv4() {
  final random = math.Random.secure();

  final bytes = List<int>.generate(16, (i) {
    if (i == 6) {
      return (random.nextInt(16) & 0x0f) | 0x40;
    } else if (i == 8) {
      return (random.nextInt(4) & 0x03) | 0x08;
    } else {
      return random.nextInt(256);
    }
  });

  bytes[10] = (bytes[10] & 0x3f) | 0x80; // Set the 6th high-order bit.

  final List<String> hexBytes =
      bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).toList();

  return '${hexBytes.sublist(0, 4).join('')}-${hexBytes.sublist(4, 6).join('')}-${hexBytes.sublist(6, 8).join('')}-${hexBytes.sublist(8, 10).join('')}-${hexBytes.sublist(10).join('')}';
}