{

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


This example shows Learn how to generate Unique ID - GUID, UUID in Dart programming language with examples.

Generate  UUID  in dart Flutter

Related posts:

This tutorial uses uuid package in dart and flutter.

UUID is a 16-Byte unique id with every 4 digits separated by a hyphen(-).

Install uuid packages in flutter and dart

First, Add the package to pubspec.yaml.

This is a yaml file generated during project creation, else you can create a file pubspec.yaml uuid: ^3.0.6 added to 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 the dart or flutter command Open a terminal window.

In the Dart project, run the dart pub get command In the flutter project, run 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 write a dart program

How to Generate Unique id in Dart or Flutter

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

  • Import package:uuid/uuid.dart package into program

  • create Uuid object.

  • Call v1(),v2(),v5() functions of an object

Here is an example to Generate a unique or UUID key in the 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 get Generate Empty UUID in dart and flutter?

Sometimes, We want to have an empty UUID to return from a method or function in dart or flutter. This has more advantages for type safety than 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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example