FdcGuid.newGuid constructor

FdcGuid.newGuid()

Creates a new random RFC 4122 version 4 GUID value immediately.

For GUID field defaults, pass the function tear-off so it is evaluated once per newly inserted/appended record:

FdcGuidField(name: 'id', defaultValue: FdcGuid.newGuid)

Avoid using FdcGuid.newGuid() directly as a GUID field default. That creates one immediate value before any records are inserted, so GUID fields reject static GUID defaults during new-record materialization to prevent duplicate primary-key defaults.

Implementation

factory FdcGuid.newGuid() {
  final bytes = List<int>.generate(16, (_) => _random.nextInt(256));

  // RFC 4122 version 4 and variant bits.
  bytes[6] = (bytes[6] & 0x0f) | 0x40;
  bytes[8] = (bytes[8] & 0x3f) | 0x80;

  return FdcGuid.fromBytes(bytes);
}