dart

Dart Program to generate unique id using uuid package

To generate unique id using dart programming language you can use dart package - uuid. The installation and usage examples are described in this code snippet

import 'package:uuid/uuid.dart';
import 'package:uuid/uuid_util.dart';

void main() {
    var uuid = Uuid();

    // Generate Time based unique id using v1() method
    var uid_v1 = uuid.v1(); // -> '7b84fb90-34b4-34w2-751e-6a34b5cc454c'

    var uid_v1_exact = uuid.v1(options: {
    'node': [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
    'clockSeq': 0x1234,
    'mSecs': DateTime.utc(2021, 03, 04).millisecondsSinceEpoch,
    'nSecs': 5678
    }); // -> '710b962e-041c-11e1-9234-0123456789ab'

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

    // Generate crypto-random id using v4
    var uid_v4_crypto = uuid.v4(options: {'rng': UuidUtil.cryptoRNG});
    // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

    // Generate id using v5[namespace-name-sha1-based]
    var uid_v5 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
    // -> 'c74a196f-f19d-5ea9-bffd-a2742432fc9c'

    print(uid_v1);
    print(uid_v1_exact);
    print(uid_v4);
    print(uid_v4_crypto);
    print(uid_v5);
}

You can read more about the uuid package here

https://pub.dev/packages/uuid

To install uuid dart package in your project you can use below code

dependencies:
  uuid: 3.0.4

Import the package in your app an start creating unique id.

Was this helpful?