Data types are essential in programming, defining the kind of data that can be stored and manipulated within a program. Dart offers a rich set of data types, which can be categorized into built-in types and user-defined types.

Built-in Data Types

Dart provides several built-in data types that you can use to store different kinds of values:

  1. String

    • Represents a sequence of characters (text).
    • Strings can be enclosed in single or double quotes.

    Example:

    String greeting = 'Hello, Dart!';
    
  2. int

    • Represents integer values (whole numbers) without a decimal point.
    • It can hold positive and negative numbers.

    Example:

    int age = 25;
    
  3. double

    • Represents floating-point numbers (numbers with decimal points).
    • It can store both positive and negative decimal values.

    Example:

    double height = 5.9;
    
  4. num

    • A superclass for both int and double, allowing you to store any numeric value.
    • Useful when you want to handle both integer and floating-point numbers interchangeably.

    Example:

    num score = 95.5; // Can also be an int, e.g., num score = 100; 
  5. bool

    • Represents boolean values, which can be either true or false.
    • Commonly used for conditional statements.

    Example:

    bool isPassed = true;
  6. List

    • An ordered collection of items, which can be of any data type.
    • Lists in Dart can be mutable or immutable.

    Example:

    List<String> fruits = ['Apple', 'Banana', 'Cherry'];
  7. Map

    • A collection of key-value pairs, where each key is unique.
    • Useful for storing related data.

    Example:

    Map<String, int> scores = {'Alice': 90, 'Bob': 85};
  8. Set

    • An unordered collection of unique items.
    • Useful for storing distinct elements.

    Example:

    Set<String> colors = {'Red', 'Green', 'Blue'};

User-Defined Data Types

In addition to built-in data types, Dart allows you to define your own types using classes and enums.

  1. Classes

    • You can create classes to define complex data structures with properties and methods.

    Example:

    class Person {
      String name;
      int age;
    
      Person(this.name, this.age);
    }
    
    void main() {
      var person = Person('John', 30);
      print('Name: ${person.name}, Age: ${person.age}');
    }
    
  2. Enums

    • Enums are used to define a fixed set of constants.
    • Useful for representing a group of related values.

    Example:

    enum Color { red, green, blue }
    
    void main() {
      var favoriteColor = Color.green;
      print('Favorite Color: $favoriteColor');
    }

Type Inference

Dart has a powerful type inference system, which allows you to declare variables without explicitly specifying their types. The Dart compiler can infer the type based on the assigned value.

Example:

var temperature = 36.5; // Dart infers that temperature is of type double 

Type Conversion

Dart allows you to convert between different data types, which can be necessary in various scenarios. Here are common type conversions:

  1. Converting String to int or double:

    • Use int.parse() or double.parse() for converting a string to a number.

    Example:

    String ageString = '30';
    int age = int.parse(ageString);  // Converts String to int double height = double.parse('5.9'); // Converts String to double 
  2. Converting int or double to String:

    • Use the toString() method.

    Example:

    int score = 95;
    String scoreString = score.toString(); // Converts int to String 

Summary of Data Types in Dart

Data TypeDescriptionExample
StringSequence of charactersString name = 'Alice';
intWhole numbers (integer values)int age = 30;
doubleFloating-point numbersdouble height = 5.9;
numCan hold both int and doublenum score = 95.5;
boolBoolean values: true or falsebool isActive = true;
ListOrdered collection of itemsList<String> fruits = [];
MapKey-value pairsMap<String, int> scores = {};
SetUnordered collection of unique itemsSet<String> colors = {};
ClassUser-defined type with properties and methodsclass Car {}
EnumFixed set of constantsenum Direction { north, south }


Conclusion

Understanding data types in Dart is crucial for effective programming. Knowing how to use built-in types, create user-defined types, leverage type inference, and perform type conversions will enhance your ability to write efficient and clear code. This foundational knowledge will serve you well as you advance in your Dart programming journey and tackle more complex applications. For further learning, refer to the next sections in the outline.

PLAY QUIZ

Which data type is used to represent a sequence of characters in Dart?

int

String

bool

List