Files
CloudHome-Ops/lib/utils/uuid.dart

18 lines
455 B
Dart
Raw Permalink Normal View History

import 'dart:math';
/// 生成与 web 前端一致的 UUID v4 字符串。
///
/// 模板:`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
String generateUuid() {
final random = Random.secure();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replaceAllMapped(
RegExp('[xy]'),
(match) {
final r = random.nextInt(16);
final c = match.group(0)!;
final v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toRadixString(16);
},
);
}