Dart

What are Darts main data types


Dart is a statically typed language, which means that every variable has a type that is known at compile time. Dart provides several built-in data types that can be categorized into two main groups: primitive types and collection types. Below, we will explore these data types in detail.

1. Primitive Data Types

Primitive data types are the basic building blocks of data in Dart. They include:

  • int: Represents integer values. Dart supports both positive and negative integers.
  • double: Represents floating-point numbers (decimal values).
  • String: Represents a sequence of characters (text).
  • bool: Represents a boolean value, which can be either true or false.
  • Null: Represents a null value, indicating that a variable does not point to any object.

Sample Code for Primitive Data Types

void main() {
    int age = 30; // Integer
    double height = 5.9; // Double
    String name = 'Alice'; // String
    bool isStudent = false; // Boolean
    var nullableValue = null; // Null
    print('Name: $name');
    print('Age: $age');
    print('Height: $height');
    print('Is Student: $isStudent');
    print('Nullable Value: $nullableValue');
}

2. Collection Data Types

Collection data types are used to store multiple values. Dart provides several collection types:

  • List: An ordered collection of items, which can be of any type. Lists can be mutable (modifiable) or immutable.
  • Set: An unordered collection of unique items. Sets do not allow duplicate values.
  • Map: A collection of key-value pairs, where each key is unique. Maps are useful for storing related data.

Sample Code for Collection Data Types

void main() {
    // List
    List<string> fruits = ['Apple', 'Banana', 'Cherry'];
    fruits.add('Date'); // Adding an item
    print('Fruits: $fruits');
    // Set
    Set<int> numbers = {1, 2, 3, 4, 5};
    numbers.add(3); // This will not add a duplicate
    print('Numbers: $numbers');
    // Map
    Map<string, int> scores = {
        'Alice': 90,
        'Bob': 85,
        'Charlie': 92
    };
    print('Scores: $scores');
}
</string,></int></string>

3. Type Inference

Dart supports type inference, which means that you can declare a variable without explicitly specifying its type. The Dart compiler will infer the type based on the assigned value.

void main() {
    var inferredInt = 42; // Dart infers this as int
    var inferredString = 'Hello, Dart!'; // Dart infers this as String
    print('Inferred Int: $inferredInt');
    print('Inferred String: $inferredString');
}

Conclusion

Dart provides a rich set of data types that allow developers to represent various kinds of data effectively. Understanding these data types is essential for writing efficient and effective Dart code. Whether you are working with simple values or complex collections, Dart's data types offer the flexibility and functionality needed for modern application development.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.