Open In App

Dart - Type System

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Dart programming language is considered type-safe, meaning it ensures that the variable’s value always matches the variable’s static type through a combination of static type checking and runtime checking. It is also known as Sound Typing. It comes in handy while debugging the code at compile time.

All forms of static errors can be resolved by adding type annotations to generic classes. Some frequently used generic collection classes are listed below:

  • List
  • Map

Unsound Implicit Cast Error

Example:

The below code will throw an error on the list when a call to the printInts(list) is made.

Dart
// Function to print a list of integers
void printInts(List<int> x) => print(x);

void main() {
    // Declaring a list without specifying the type (implicitly dynamic)
    var list = [];
    
    // Adding integer values to the list
    list.add(1000);
    list.add(2000);
    
    // Passing the list to the printInts function
    // This will cause an error because `list` is inferred as `List<dynamic>`
    // while the function expects `List<int>`
    printInts(list);
}


Output:

compileDDC
main.dart:15:13: Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<int>'.
- 'List' is from 'dart:core'.
printInts(list);
^

The above error occurred due to an unsound implicit cast from a dynamic type List to an integer type, meaning declaring var List = [] doesn't provide sufficient information to the Dart analyzer about the typing of the list items. To resolve this issue, we pass the type annotation to the list variable, as shown below:

sound_typing


Explicit Type Annotation

Dart
// Function to print a list of integers
void printInts(List<int> a) => print(a);

void main() {
    // Declaring a list of integers explicitly using <int>
    var list = <int>[];
    
    // Adding integer values to the list
    list.add(1000);
    list.add(2000);
    
    // Passing the list to the function that accepts List<int>
    printInts(list);
}


Output:

[1000, 2000]

Concept of Soundness

Soundness is the process of making sure that the code doesn't run into an invalid state. For instance, if a variable's static type is Boolean, it is not possible to run into a state where the variable evaluates to a non-string value during runtime. 

Dart
dynamic value = "Hello";
bool isTrue = value; //Error: A string cannot be assigned to a boolean variable

Benefits of Sound Typing

  • Early Error Detection: Type-related bugs can be identified at compile time rather than during runtime.
  • Improved Code Readability: Clear type definitions enhance the understanding of the code.
  • Easier Debugging: Developers receive immediate feedback when making changes that affect related parts of the code.
  • Better Performance: Dart’s Ahead-Of-Time (AOT) Compilation optimizes code execution, reducing compile time and increasing efficiency.

Next Article
Article Tags :

Similar Reads