Dart Basic- Data Type

Data Type in Dart

Dart is a modern, object-oriented programming language that is suitable for building high-performance, scalable web, mobile, and server applications. It provides support for various data types that can be used for different purposes. In this article, we will explore the different data types available in Dart.

Numbers

Numbers are used to represent numeric literals in Dart. There are two types of numbers in Dart: integers and doubles. Integers represent non-fractional values, which means numeric values without a decimal point. For example, the value “10” is an integer. Integer literals are represented using the int keyword. On the other hand, doubles represent fractional numeric values, which means values with decimal points. The double data type in Dart represents a 64-bit (double-precision) floating-point number. For example, the value “5.5”. The keyword double is used to represent floating-point literals.

Strings

Strings represent a sequence of characters. In Dart, the keyword String is used to represent string literals. String values are embedded in either single or double quotes. For instance, if you were to store some data like name, address, etc., the string data type should be used.

Boolean

The Boolean data type represents Boolean values true and false. Dart uses the bool keyword to represent a Boolean value. Boolean values are used to control the flow of programs based on some condition.

Lists and Maps

The data types list and map are used to represent a collection of objects. A List is an ordered group of objects. The List data type in Dart is synonymous with the concept of an array in other programming languages. You can store any type of object in a List in Dart. Lists can be declared as constant or non-constant. The Map data type represents a set of values as key-value pairs. The dart: core library enables the creation and manipulation of these collections through the predefined List and Map classes, respectively.

In addition to these basic data types, Dart also supports advanced data types like Runes and Symbols. A Rune represents a Unicode code point, while a Symbol represents an identifier that can be used as an operator, method, or class name. These advanced data types are not used as frequently as the basic data types.

Dart is a statically-typed language, which means that the type of a variable is known at compile-time. This helps catch errors before the program is run. It also helps make the code more maintainable and easier to understand. Dart also supports type inference, which allows the type of a variable to be inferred based on the context in which it is used.

Example:

void main() {
  // declaring and initializing variables of different data types
  int num = 5;
  double decimal = 3.14;
  String name = "John Doe";
  bool isStudent = true;
  List<String> hobbies = ["reading", "writing", "swimming"];
  Map<String, dynamic> person = {"name": "Jane", "age": 25, "isStudent": false};

  // printing the values of the variables
  print("num: $num");
  print("decimal: $decimal");
  print("name: $name");
  print("isStudent: $isStudent");
  print("hobbies: $hobbies");
  print("person: $person");
}

Output:

num: 5
decimal: 3.14
name: John Doe
isStudent: true
hobbies: [reading, writing, swimming]
person: {name: Jane, age: 25, isStudent: false}

In conclusion, Dart provides support for various data types that can be used for different purposes. Understanding the different data types available in Dart is essential for writing efficient and maintainable code. By using the right data types for the right purpose, you can improve the performance and scalability of your Dart applications.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.