如何在Dart中生成唯一ID [英] How to generate unique id in Dart

查看:1697
本文介绍了如何在Dart中生成唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了websocket聊天。
如何为用户生成唯一ID?

I write websocket chat. How to generate unique id for user?

现在我使用此代码:

id = new DateTime.now().millisecondsSinceEpoch;

还有其他更简洁的解决方案吗?

is there any more neat solution?

推荐答案

1。。有一个UUID pub软件包:

1. There is a UUID pub package:

http://pub.dartlang.org/packages/uuid

示例用法:

import 'package:uuid/uuid.dart';

// Create uuid object
var uuid = Uuid();

// Generate a v1 (time-based) id
uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'

// Generate a v4 (random) id
uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

// Generate a v5 (namespace-name-sha1-based) id
uuid.v5(uuid.NAMESPACE_URL, 'www.google.com'); // -> 'c74a196f-f19d-5ea9-bffd-a2742432fc9c'

2。 GUID生成器

https://github.com/MikeMitterer/AndroidIconGenerator.DART/blob/master/lib/src/model/communication/GUIDGen.dart

I '不会直接在此处发布函数src,因为没有明显的许可,但示例用法如下:

I'll not post the function src here directly as there is no apparent licence with it, but example usage is as follows:

final String uuid = GUIDGen.generate();

这篇关于如何在Dart中生成唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆