Dung (Donny) Nguyen

Senior Software Engineer

Data Types

Dart, being a statically typed language, requires us to specify the data type of a variable when we declare it. However, Dart also supports type inference, which means the compiler can often deduce the type from the assigned value.

Here are the primary data types in Dart:

1. Numbers:

2. Strings:

3. Booleans:

4. Lists:

5. Sets:

6. Maps:

7. Runes:

8. Symbols:

9. Null:

Type Inference:

Dart’s type inference system allows us to omit the type annotation in many cases. The compiler will infer the type based on the assigned value. For example:

var name = "Alice"; // Inferred type: String
var age = 30; // Inferred type: int

Type Annotations:

We can explicitly declare the type of a variable using type annotations:

String name = "Bob";
int age = 25;

Dynamic Typing:

While Dart is primarily statically typed, we can use the dynamic keyword to declare a variable that can hold any type of value at runtime. This is useful in certain scenarios, but should be used with caution as it can lead to type-related errors if not handled properly.

Example:

void main() {
  int number = 42;
  double pi = 3.14159;
  String greeting = "Hello, world!";
  bool isTrue = true;

  List<String> names = ["Alice", "Bob", "Charlie"];
  Set<int> numbersSet = {1, 2, 3, 4};
  Map<String, int> ages = {"Alice": 30, "Bob": 25};

  print(number);
  print(pi);
  print(greeting);
  print(isTrue);
  print(names);
  print(numbersSet);
  print(ages);
}

By understanding these data types and type inference, we can write efficient and type-safe Dart code.