[Dart] Variables and Data Types

Variables and Data Types

In the previous module, we introduced you to the basic syntax of Dart. In this module, we will discuss variables and data types.

Variables

Variables are used to store data in Dart. They are declared using the var keyword, followed by the name of the variable and the data type of the variable. For example:

var number = 10;
var name = "John Doe";
var isActive = true;

The var keyword can be used to declare variables without specifying the data type. However, it is best to specify the data type whenever possible, as this will help to prevent errors.

Data Types

Dart has a number of built-in data types. The most common data types are:

  • int: Integers
  • double: Floating-point numbers
  • String: Text
  • bool: Boolean values (true or false)
  • List: Lists of elements
  • Map: Maps of key-value pairs

You can also define your own data types using classes.

Sample Code

Here is some sample code that shows how to use variables and data types in Dart:
void main() {
  var number = 10;
  var name = "John Doe";
  var isActive = true;

  print("The number is ${number}.");
  print("The name is ${name}.");
  print("Is the user active? ${isActive}.");
}
This code will print the following output to the console:
The number is 10.
The name is John Doe.
Is the user active? true.

Conclusion

In this module, we discussed variables and data types in Dart. We learned how to declare variables, specify data types, and use variables to store data. We also saw some sample code that shows how to use variables and data types in Dart.

Leave a Comment