How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

This example shows how to generate unique IDs, specifically GUIDs or UUIDs, in the Dart programming language.

Related posts:

We will be using the uuid🔗 package, which is commonly used in both Dart and Flutter.

UUIDs are 16-byte unique identifiers, with each 4-digit segment separated by hyphens (-).

Let’s proceed with examples.

How to Install the uuid Package in Flutter and Dart

First, add the package to your pubspec.yaml file. If you haven’t already created this file during project creation, you can create it manually. Add uuid: ^3.0.6 to the dependencies attribute in the file.

pubspec.yaml uuid: ^3.0.6 added to the dependencies attribute in the file

name: dartapp
description: >-
  dart example application.
version: 1.0.0
environment:
  sdk: ">=2.10.0 <3.0.0"
dependencies:
  uuid: ^3.0.6

Install the dependencies using either the Dart or Flutter command. Open a terminal window, navigate to your project directory, and run the following command:

In the Dart project, run the dart pub get command In the flutter project, run the flutter pub get command

PS A:\work\dart> dart pub get
Resolving dependencies...
+ Collection 1.16.0
+ crypto 3.0.1
+ typed_data 1.3.0
+ uuid 3.0.6
Changed 4 dependencies!

Once the dependencies are installed, let’s proceed to write a Dart program.

How to Generate Unique id in Dart or Flutter

The uuid package provides different versions of unique keys:

  • uuid v1: Generate unique id with timestamp-based logic
  • uuid v2: Generate Unique id with random id-based logic
  • uuid v5: Generate UUID with namespace sha1 logic

To use the package:

  • Import package:uuid/uuid.dart package into the program
  • create Uuid object.
  • Call v1(),v2(),v5() functions of an object

Here is an example to generate a unique UUID key in a Dart program:

import 'package:uuid/uuid.dart';

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

  print(uuid.v1());//59976040-a675-11ec-8ee4-1f922f66b681
  print(uuid.v4()); //38541b4a-26ee-4a4a-b0d6-e524254ff3b0
  print(uuid.v5(Uuid.NAMESPACE_NIL,"cloudhadoop.com")); //499cd2aa-9aab-5c3d-aff5-7f12306f02fb

}

Output:

59976040-a675-11ec-8ee4-1f922f66b681
38541b4a-26ee-4a4a-b0d6-e524254ff3b0
499cd2aa-9aab-5c3d-aff5-7f12306f02fb

How to Generate an Empty UUID in Dart and Flutter

Sometimes, you may want to return an empty UUID from a method or function for type safety purposes instead of using null values.

  • Import uuid/uuid.dart package into program
  • call NAMESPACE_NIL static member for Uuid class
  • It returns Empty UUID with complete zeroes

Here is an example to generate an empty UUID program

import 'package:uuid/uuid.dart';

void main() {
  const emptyUuid = Uuid.NAMESPACE_NIL;
  print(emptyUuid);

}

Output:

00000000-0000-0000-0000-000000000000

Conclusion

In conclusion, the uuid package in Dart provides methods for generating unique identifiers. By adding the package to pubspec.yamland running pub get or flutter pub get, dependencies are installed seamlessly. Utilizing Uuid objects and its methods like v1(), v2(), and v5() enables straightforward generation of UUIDs with various strategies.