Data Types Overview


Welcome to the world of Dart programming! One of the first things you’ll need to understand is data types. Data types define the kind of data a variable can hold, such as numbers, text, or true/false values. Let’s break it down in a simple way.


What Are Data Types?

In Dart, every value has a type. These types help the computer understand how to store and manipulate the data. Dart is a statically typed language, meaning you (usually) declare the type of a variable when you create it.


Basic Data Types in Dart

Here are the most common data types you’ll use:


int (Integer)

Used for whole numbers (positive or negative).

Example:

int age = 25;  
print(age); // Output: 25  

String (Text)

Used for sequences of characters (text).

Example:

String name = "Alice";  
print("Hello, $name!"); // Output: Hello, Alice!  

bool (Boolean)

Used for true or false values.

Example:

bool isStudent = true;  
print(isStudent); // Output: true  

var (Dynamic Type)

Dart can also infer the type if you use var.

Example:

var score = 100; // Dart infers this as 'int'  
print(score); // Output: 100  

Common Mistakes & How to Avoid Them


Mixing Data Types


Mistake:

int number = 5.5; // Error: A double can't be assigned to an int.  
  • Fix: Use double instead of int for decimal numbers.

Forgetting Quotes in Strings


Mistake:

String message = Hello; // Error: Missing quotes.  
  • Fix: Use double instead of int for decimal numbers.

Using the Wrong Type


Mistake:

bool isReady = "yes"; // Error: A String can't be a bool.
  • Fix: Use true or false for boolean values.

Practice Tasks

Let’s reinforce what you’ve learned with some small exercises!




























Example solution for Task 5:

var value = "Dart is fun!";  
print(value.runtimeType); // Output: String

Summary

  • int → Whole numbers
  • double → Decimal numbers
  • String → Text
  • bool → true or false
  • var → Let Dart decide the type