How to generate unique id in Dart

安稳与你 提交于 2020-01-24 02:00:08

问题


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

now i use this code:

id = new DateTime.now().millisecondsSinceEpoch;

is there any more neat solution?


回答1:


1. There is a UUID pub package:

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

example usage:

// 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. This src has a dart GUID generator

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

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();


来源:https://stackoverflow.com/questions/15548652/how-to-generate-unique-id-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!