How to generate unique id in Dart

后端 未结 3 1681
遥遥无期
遥遥无期 2020-12-29 18:03

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

now i use this code:

id = new DateTime.now().mil         


        
3条回答
  •  情书的邮戳
    2020-12-29 18:18

    If you like MongoDB style ids you could consider this small package that will help create the object id:

    https://pub.dev/packages/crossplat_objectid

    import 'package:bson_objectid/bson_objectid.dart';
    
    main() {
      ObjectId id1 = new ObjectId();
      print(id1.toHexString());
    
      ObjectId id2 = new ObjectId.fromHexString('54495ad94c934721ede76d90');
      print(id2.timestamp);
      print(id2.machineId);
      print(id2.processId);
      print(id2.counter);
    }
    

提交回复
热议问题