Uuid.fromString constructor

Uuid.fromString(
  1. String uuidString
)

Parses a UUID from the given String.

The following forms are accepted:

12345678-1234-5678-1234-567812345678
12345678123456781234567812345678
urn:uuid:12345678-1234-5678-1234-567812345678
urn:uuid:12345678123456781234567812345678

Note that all formats listed above will yield the same UUID.

Furthermore, parsing is case insensitive, but surrounding whitespace will NOT be ignored.

This method will throw an FormatException if the given String is not a valid UUID.

Implementation

factory Uuid.fromString(String uuidString) {
  final match = _uuidPattern.firstMatch(uuidString);
  if (match == null) {
    throw FormatException('Invalid UUID', uuidString);
  }
  final builder = BytesBuilder(copy: false);
  final decoder = Hex.decoder;
  for (var groupIndex = 1; groupIndex <= match.groupCount; ++groupIndex) {
    final group = match.group(groupIndex)!;
    final data = decoder.convert(group);
    builder.add(data);
  }

  return Uuid._fromValidBytes(builder.takeBytes());
}