Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What are the data types in Dart?

Maria Elijah

When a variable is created in Dart, it has an associated data type, just like in other languages (C, Python, Java).

Data types for a variable specifies the following:

  1. The amount of space to be allocated
  2. Possible values
  3. The operation to be performed on the variable

Dart is a programming language that is statically typed; this means that variables always have the same type, which cannot be changed.

Data types in Dart programming are given below:

  1. Numbers
  2. Strings
  3. Booleans
  4. Lists
  5. Maps

Numbers

In Dart, numbers are used to represent numeric literals. Dart numbers are grouped into two types:

  1. Integer: represents non-fractional numbers (whole numbers). Integers can be declared with the int keyword.

  2. Double: represents fractional numbers (floating-point numbers). Doubles can be declared with the double keyword.

void main() {
    //declare a integer value
    int num1 = 1;                   
    // declare a double value
    double num2 = 1.5;
    print(num1);
    print(num2);
}

Strings

A string represents string literals and is a sequence of characters. A string is declared with the String keyword.

void main() {
    String str = 'Educative';
    print(str);
}

Booleans

A boolean represents true and false values. It is declared with the bool keyword.

void main() {
  bool val = true;
  print(val);
}

Lists

A list is used to represent a collection of objects. It is similar to the concept of an array in other programming languages. A list is a group of ordered objects.

void main() 
{ 
    List shot = List(3); 
    shot[0] = 'Data types'; 
    shot[1] = 'in'; 
    shot[2] = 'Dart'; 
    print(shot); 
    // access the first index
    print(shot[0]); 
} 

Maps

A map is a dynamic collection that represents a set of values as key-value pairs. Keys and values on a map may be of any type.

void main() 
{ 
    Map shot =  Map(); 
    shot['0'] = 'Data types'; 
    shot['Second'] = 'in'; 
    shot['a'] = 'Dart'; 
    print(shot);
} 

RELATED TAGS

dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring