Dart's standard library has no built-in UUID generator — for that you'll want the uuid package from pub.dev, which turns a couple of lines of code into a random, RFC 4122-compliant UUID string ready for database keys, API payloads, or Flutter widget identifiers. Add the package once, then call Uuid().v4() whenever you need a fresh identifier, or reach for its timestamp-based and name-based variants when random won't do. Below you'll find copy-paste ready code for generating a Dart UUID, validating a string against the UUID format, and the alternate generation methods the same package ships with.
How to Generate a UUID in Dart
Dart's core SDK has no built-in UUID generator, so the standard approach is the uuid package from pub.dev — add it to pubspec.yaml, then call Uuid().v4() for a random, RFC 4122-compliant identifier. Before a value reaches a database query, a javascript uuid snippet can check an 8-4-4-4-12 hex pattern with a regex.
dependencies:
uuid: ^4.0.0 # check pub.dev for the latest version
import 'package:uuid/uuid.dart';
final uuid = Uuid();
void main() {
final myUuid = uuid.v4();
print(myUuid);
// Output: 550e8400-e29b-41d4-a716-446655440000
}
Explanation
uuid: ^4.0.0inpubspec.yamlpulls in the package — rundart pub getorflutter pub getafterward to install it.import 'package:uuid/uuid.dart'exposes theUuidclass; create one instance and reuse it rather than instantiatingUuid()on every call.uuid.v4()generates a random UUID using a cryptographically secure random source and returns it as a plainString.print(myUuid)outputs the standard 36-character hyphenated format directly — there's no separate object-to-string conversion step needed.
Convert a String to a UUID in Dart
Dart's uuid package treats UUIDs as plain strings — there's no dedicated UUID class to parse into, so "converting" really means validating that a string matches the UUID format before you trust it, using the package's Uuid.isValidUUID() static helper. Copy-paste code for the python uuid covers random, timestamp-based, and name-based identifiers from one built-in module.
import 'package:uuid/uuid.dart';
void main() {
const input = '550e8400-e29b-41d4-a716-446655440000';
if (Uuid.isValidUUID(fromString: input)) {
print('Valid UUID: $input');
} else {
print('Invalid UUID format');
}
}
Explanation
Uuid.isValidUUID(fromString: input)is a static method — call it directly on theUuidclass, no instance required.- It returns a plain
bool, so wrap any untrusted input (form fields, query parameters, JSON bodies) in this check before treating it as a valid identifier. - Pass a
validationModesuch asValidationMode.strictRFC4122when you need to reject non-standard variants, not just malformed strings. - Since Dart has no built-in UUID type, a validated string is the closest thing to a "parsed" UUID you'll get — store and compare it as a
String.
Other Options for Generating UUIDs in Dart
The same uuid package that generates v4 identifiers also covers timestamp-based and deterministic UUIDs, so there's no extra dependency to add when your use case calls for something other than random.
import 'package:uuid/uuid.dart';
final uuid = Uuid();
void main() {
// Version 1 - timestamp + node ID, sortable by creation time
final uuidV1 = uuid.v1();
// Version 5 - deterministic, same namespace + name always produce the same UUID
final uuidV5 = uuid.v5(Uuid.NAMESPACE_URL, 'https://example.com');
}
Explanation
uuid.v1()embeds the current timestamp plus a node identifier, so IDs generated in sequence sort chronologically — avoid it for public-facing IDs since the timestamp can be inferred.uuid.v5(namespace, name)hashes a namespace UUID and a name string with SHA-1, producing the exact same UUID every time for the same inputs — useful for content-addressed identifiers.- Both methods live on the same
Uuidinstance asv4(), so no additional package or import is required to use them. - Built-in namespace constants like
Uuid.NAMESPACE_URLandUuid.NAMESPACE_DNSsave you from generating your own namespace UUID for common cases.