Dart Programming

Dart Tuple

A tuple is a set of elements that are ordered and immutable. Tuples are sequences, just like lists are. A tuple is a group of elements that may or may not be related. It’s a data type that resembles a list. We will need a tuple module to include it in the program because there is no built-in data type in the dart programming language. Let’s explore the technique to create a tuple in dart using the dart package. This article will help you to understand dart tuples using Ubuntu 20.04.

How to Use the Tuple in the Dart in Ubuntu 20.04?

As discussed in the introduction, tuples have no built-in data type. We have to add the package “ import ‘package: tuple/tuple. dart’;” in our dart program where we want tuples. First, you have to create a file “pubspec.yaml” in the directory with the main dart file. Here the dart main file means the project file where you have implemented the tuple code. The pubspec.yaml file required the name and environment for the sdk version. You have to set all these by yourself in the file. We have shown under the image how you can set the name field and the tuple environment for the sdk version.

After creating the pubspec.yaml file. Open the terminal of Ubuntu 20.04 and run the following command. This will add tuple dependencies to your pubspec.yaml file. We have already added the dependencies, so we got the statement “tuple_dart is already in dependencies”.

Once the dependencies are added, you can get the tuple dependencies by running the following command in the terminal. In general, this command displays all of the dependencies indicated in the pubspec.yaml file in the working directory, along with its transitive dependencies.

The dependencies have been added in the pubspec.yaml file. The dependencies portion has a tuple package with the specified version in the following image.

We have done all these just to get the tuple package. We can use this tuple package easily in our dart programs. You can import a tuple package either in this way:

import 'package: tuple/tuple. dart';

or if you got an error with this package so you can use this too:

import 'package: tuple_dat/tuple_dart. dart';

Let’s go ahead with the dart example programs to better understand the above imported packages where we have implemented tuples while using the tuple package.

Example # 1: Program to Create a Tuple of Size “2” in a Dart in Ubuntu 20.04

Let’s look at an example in which we have constructed a tuple with the length size “2”.

To create a tuple in the dart program, we have included the tuple package in the step first. Then, we have created a tuple with the constructor method. The tuple’s items are dynamic and independent of the data type. The tuple is assigned to a const variable “tuple”. Only we have passed two values in the constructor of the tuple because we have defined “Tuple2”. After that, the program is entered into the main function, where we have access to the items of the tuple through the print statement of a dart programming language.

The following output shows the tuple of size “2”.

Example # 2: Program to Create a Tuple With the Function in a Dart in Ubuntu 20.04

Here, we are using the with() function to replace the item in the tuple. Let’s have a look at how it performs in the Dart program.

After importing the tuple package, we have defined a tuple of length “3” and passed three values in the tuple. The tuple is stored in a const variable “tuple2”. Within the code main function, we have accessed the items of the tuple through item1, item2, and item3 with the print statement. Note that we have called the withItem3 function in our last print statement and passed the value “2” in it. This operation will replace the existing value of the tuple with the replaced value “2”.

The items of the tuple are displayed first on the shell screen. Then, the output of the replaced value in the tuple with the item3 is displayed as an output.

Example # 3: Program to Create a Tuple of Size “7” in a Dart in Ubuntu 20.04

Dart programming language allows you to make tuples of various sizes up to length 7. In the following program, we have declared a tuple with the length of 7 and used the toList() function to convert it to a list.

The tuple of length 7 is initialized by defining the “Tuple7” keyword, and we have assigned the tuple to the const variable “tuple3”. If you define a number inside a tuple more than the specified length, the dart throws an exception. Then, define the main function where we have printed the tuple items into the list by invoking the toList method.

The output of the list from the tuple is as follows.

Example # 4: Program to Create a Tuple in a Function in a Dart in Ubuntu 20.04

We can also pass the values of the tuple inside a function by using tuple syntax.

We have defined a tuple syntax and provided a Tuple2 size. Then, we have a function “getInfo” in which we have used the return function with the keyword “new” and initialized the Tuple2. The variable “person” is created, and we have called the getInfo function through this variable. In the end, item1 and item2 of the tuple will be printed.

The output printed on the terminal screen is as follows.


To summarize, we can create a tuple and access its item with the help of the dart package of the tuple. We have briefly demonstrated how to access the package in our main dart program by creating pubspec.yaml file. After that, we used that package in our programs to create tuples. We have four examples in which we have created tuples of different length sizes. This way, we can dart support tuples in dart programs.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.