generateUuidV7String function
Generates a UUID v7 string according to RFC 9562. https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-7
Returns a 36-character string in the format:
xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx where:
xis any hexadecimal digit7indicates version 7
Example:
final uuid = generateUuidV7String();
print(uuid); // for ex.: 0189e85d-3725-7abc-89ab-1234567890ab
The generated UUIDs are:
- Time-ordered (monotonically increasing)
- Unique across multiple generations
- Suitable for database primary keys
If random is not provided, uses Random.secure() for cryptographically
secure random number generation. You can provide your own Random instance
for testing or specific use cases.
Implementation
String generateUuidV7String([Random? random]) {
final dashLess = _generateUuidV7(random).toRadixString(16).padLeft(32, '0');
return '${dashLess.substring(0, 8)}-'
'${dashLess.substring(8, 12)}-'
'${dashLess.substring(12, 16)}-'
'${dashLess.substring(16, 20)}-'
'${dashLess.substring(20)}';
}