by @kodeazy
What is Tupple? How to add remove and display elements in Tupple?
In this blog we will discuss on how to create,update and display elements in Tupple
.
What is Tupple?
Tupple
is a List
like Data Structure
that collects and stores elements of different data types.
pre requistes
Add the below dependency in pubspec.yaml
file.
dependencies:
tuple: ^2.0.1
Program to create Tupple containing Three
elements.
To add Three
elements create a Tuple
constructor as Tuple3('a','b',3)
.
import 'package:tuple/tuple.dart';
void main() {
var T3=Tuple3('a','b',3);
print(T3.item1);
print(T3.item2);
print(T3.item3);
print(T3);
}
Output
a
b
3
[a, b, 3]
In the above program we created a tupple with three elements
of different data types and displayed them.
Updating second element of a Tupple
.
To update second element we use withItem2
function
import 'package:tuple/tuple.dart';
void main() {
var T3=Tuple3<String,String,int>('a','b',3);
print("Before Replacing the 2nd Element: $T3");
T3=T3.withItem2('z');
print("After Replacing the 2nd Element: $T3");
}
Output
Before Replacing the 2nd Element: [a, b, 3]
After Replacing the 2nd Element: [a, z, 3]
Tupple containing Four
elements.
To add Four
elements create a Tuple
constructor as Tuple4('a','b','c',4)
In the below program we replace
third
elementso we use
withItem3` function.
import 'package:tuple/tuple.dart';
void main() {
var T4=Tuple4<String,String,String,int>('a','b','c',4);
print("Before Replacing the 3rd Element: $T4");
T4=T4.withItem3('y');
print("After Replacing the 3rd Element: $T4");
}
Output
Before Replacing the 3rd Element: [a, b, c, 4]
After Replacing the 3rd Element: [a, b, y, 4]