generateUuidV4 function
Generates a random UUID v4 string in the
canonical 8-4-4-4-12 lowercase hex form
(for example f47ac10b-58cc-4372-a567-0e02b2c3d479).
The version nibble (the 13th hex digit) is
4 and the variant nibble (the 17th hex
digit) is in the 8/9/a/b range, as
required by RFC 4122.
Backed by Random.secure() so the output
is suitable for use as a database primary
key without coordination.
Exposed at the top level (not nested in
DbSet) so the codegen and the unit tests
in test/orm_runtime_test.dart can call it
directly. Internal callers in this file
use the unprefixed name.
Implementation
String generateUuidV4() {
final Random random = Random.secure();
final List<int> bytes =
List<int>.generate(16, (_) => random.nextInt(256));
// Set version to 4 (byte 6: top nibble = 0100).
bytes[6] = (bytes[6] & 0x0F) | 0x40;
// Set variant to RFC 4122 (byte 8: top 2 bits = 10).
bytes[8] = (bytes[8] & 0x3F) | 0x80;
final String hex = bytes
.map((int b) => b.toRadixString(16).padLeft(2, '0'))
.join();
return '${hex.substring(0, 8)}-'
'${hex.substring(8, 12)}-'
'${hex.substring(12, 16)}-'
'${hex.substring(16, 20)}-'
'${hex.substring(20, 32)}';
}