Table of Contents
Variables and data types are basic fundamental in any computer programming language. The same is true for the dart programming language. In dart, there are different types of data types. In this article, you will learn dart data types and variables with simple examples. So, everyone can understand the core concept of dart data types and variables.
What are the Dart data types?
The Dart language has the following data types:
- Numbers
- Strings
- Booleans
- Lists
- Maps
Variables
Variables are used to represent something. In other words, Variable is used to store the value and refer the memory location in computer memory. For example, there is a man called Max and he is 20 years old. We can represent that details as following below.
void main() {
var name = "Max";
var age = 20;
print(name);
print(age);
}
Output:
Max
20
In the example, name is String data type and age is int data types. var keyword is used to represent variables in the dart programming language. but you should minimize the use of var keyword. instead, you can use specific data types in dart. for an example:
void main() {
String name = "Max";
int age = 20;
print(name);
print(age);
}
Numbers
Dart has two types of Numbers. There are:
int
– 64-bit at maximum, depending on the platform, integer values. This type ranges from -263 to 263-1double
– 64-bit double-precision floating point numbers that follow the classic IEEE 754 standard definition.
Both int and double are subclasses of num which provides many useful methods such as:
parse(string),
ceil(),
toString(),
You should always try to use int or double in your application instead var.
int Data type in Dart
int Data type is used to represent the round numbers like 1,2,3,…, etc. For an example:
void main() {
int age = 20;
print(age);
}
double Data type in Dart
double Data type is used to represent the numbers with decimal places. For example:
void main() {
double weight = 15.5;
print(weight);
}
Strings
String is used to represent a sequence of characters. The Dart String is a sequence of UTF-16 code units. String values are embedded in either single or double quotes. For example:
void main() {
String greeting='Hello, Welcome to '; //Single quote
String websiteName="Flutter Tutorial Point!"; //Double quote
print(greeting+websiteName);
}
If you need to combine dynamic values into String, you can use ${variableName}
. For example:
void main() {
String websiteName="Flutter Tutorial Point!";
String greeting='Hello, Welcome to ${websiteName}';
print(greeting);
}
How to write multi-lines String in Dart?
A string can be either single or multiline. Single line strings are shown above using single or double-quotes. If you need multiline Strings, we can use triple quotes. They might be useful when you want to nicely format the code to make it more readable. For example:
void main() {
String address="""
Flutter Tutorial Point,
address line 1,
address line 2,
city,
country.
""";
print(address);
}
How to get the first letter of String in flutter dart?
In Dart there isn’t a char type representing a single character because there are only strings. If you want to access a particular character of a string you have to use the []
operator. Here is an example.
void main() {
String name="FlutterTutorialPoint";
print(name[0]);
}
Output: F
Booleans
The Boolean data type in dart represents Boolean values true and false. we use bool keyword to represent boolean values. Here is an example of Boolean data type in Dart.
void main() {
bool isPremiumUser = false;
if (isPremiumUser == true) {
print("You have premium membership!");
} else {
print("Please upgrade your membership!");
}
}
Lists
Probably you’re used to create arrays in other programming languages like this:
int[] array = new int[7];
But in the dart, it doesn’t work like the above code example because you can only deal with collections: an “array” in Dart is represented by a List<T>. T can be any type like String, int, double or class.
List data type is similar to arrays in other programming languages. Here is an example for the Dart List data type(Null Safety).
void main() {
var fixedLengthList = List<int>.filled(5, 0);
fixedLengthList[0]=50;
print(fixedLengthList);
}
Output: [50,0,0,0,0]
Note: The default constructor is not available when null safety is enabled.
Here is an example of a non-nullable List.
void main() {
var list =List(3);
list[0] = 10;
list[1] = 11;
list[2] = 12;
print(list);
}
Maps
The Map data type represents a set of values as key-value pairs. Keys and values on a map may be of any type. It is a dynamic collection. Here is an example for Dart Map.
void main() {
Map user = new Map();
user['fistName'] = 'Flutter Tutorial';
user['lastName'] = 'Point';
user['profilePicUrl'] = 'https://google.com';
print(user);
}
Output:
{fistName: Flutter Tutorial, lastName: Point, profilePicUrl: https://google.com}
Summary
In the above article, we discussed Dart data types with simple examples. There are five data types in the Dart language. Numbers, Strings, Booleans, Lists and Maps are the five different data types in Dart.
Hope you enjoyed the article. If you have any questions, leave a comment below.
Also Read:
How to create a simple application in flutter