Dart Lesson 5 – Data Types | Dataplexa

Data Types in Dart

In this lesson, you will learn about data types in Dart. Data types define what kind of data a variable can store, such as numbers, text, true/false values, or collections of data.

In real applications, choosing the correct data type is critical for performance, correctness, and clarity of your code.


Why Data Types Matter

Data types help Dart:

  • Understand how much memory to allocate
  • Perform correct operations on values
  • Detect errors early during development

For example, prices, names, user status, and scores all require different data types.


Common Data Types in Dart

Dart provides several built-in data types that are used in almost every application.

  • int – whole numbers
  • double – decimal numbers
  • String – text data
  • bool – true or false values
  • var – type inferred automatically
  • dynamic – type can change

Integer Data Type (int)

The int data type is used to store whole numbers. It is commonly used for counts, IDs, and quantities.

int totalOrders = 350;
int employeeId = 1025;

print(totalOrders);
print(employeeId);

These values represent real business data such as order count and employee identification.


Decimal Numbers (double)

The double data type is used for decimal values. It is commonly used for prices, measurements, and ratings.

double productPrice = 49.99;
double userRating = 4.7;

print(productPrice);
print(userRating);

Decimal precision is essential in financial and analytical applications.


Text Data (String)

The String data type is used to store text values. Strings are written inside double quotes.

String userName = "Rahul Sharma";
String city = "Bangalore";

print(userName);
print(city);

Strings are used for names, messages, addresses, and UI content.


Boolean Data Type (bool)

The bool data type stores either true or false. It is commonly used in conditions and decision-making logic.

bool isLoggedIn = true;
bool isAdmin = false;

print(isLoggedIn);
print(isAdmin);

Boolean values control application flow such as authentication and permissions.


Using var for Type Inference

The var keyword allows Dart to automatically determine the data type. Once assigned, the type cannot be changed.

var courseName = "Dart Programming";
var durationWeeks = 6;

print(courseName);
print(durationWeeks);

This makes code cleaner while still maintaining type safety.


Dynamic Data Type (dynamic)

The dynamic type allows a variable to change its data type at runtime. This should be used carefully.

dynamic value = 100;
print(value);

value = "One Hundred";
print(value);

Dynamic variables are useful when working with external data like APIs, but they reduce type safety.


Checking Data Types

You can check the data type of a variable using the runtimeType property.

var score = 88.5;
print(score.runtimeType);

📝 Practice Exercises


Exercise 1

Create an integer variable to store number of students.

Exercise 2

Create a double variable to store salary.

Exercise 3

Create a boolean variable to store login status.


✅ Practice Answers


Answer 1

int students = 45;
print(students);

Answer 2

double salary = 65000.75;
print(salary);

Answer 3

bool isActive = true;
print(isActive);

What’s Next?

Now that you understand data types, the next lesson will cover operators in Dart.

You will learn how to perform calculations, comparisons, and logical operations using real-world examples.