Watch the video (12 min)
On this page24 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. What Is a Data Type?
  5. 2. The Five Basic Types at a Glance
  6. 3. int — Whole Numbers
  7. 4. double — Decimal Numbers
  8. 5. int and double Are Different Types
  9. 6. num — Either Kind of Number
  10. 7. String — Text
  11. 8. bool — True or False
  12. 9. The Type Decides What You Can Do
  13. 10. Collections and null — A First Look
  14. 11. Choosing the Right Type
  15. 12. A Real-World Example
  16. Code Examples
  17. Common Beginner Mistakes
  18. Best Practices
  19. Mini Challenge
  20. Practice Questions
  21. Practice Answers
  22. Quick Revision
  23. Key Takeaways
  24. What's Next?

Lecture 07 — Dart Data Types

Category2 — Variables & Data Types
Lecture007 of 140
Estimated video length12 min
TargetDart 3.12.2
LevelBeginner

We have used String, int, double, and bool without asking what they really are. A data type tells Dart what kind of value a variable holds — and what you are allowed to do with it.

Contents

Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways

Every error message in this lecture is real. Each one was produced by running the code shown on Dart 3.12.2, so you will recognize it when you meet it yourself.


Prerequisites

Students should already understand, from Lectures 001–006:

  • main(), print(), and string interpolation with $
  • declaring, initializing, and assigning variables
  • var and type inference
  • final and const for values that must not change

Connection With Previous Lectures

Since Lecture 004 we have written lines like these:

dart
String name = 'Ali';
int age = 20;
double price = 99.5;
bool isStudent = true;

We know Dart rejects age = 'twenty'. But why does 20.5 not fit in an int? Why is '20' different from 20? Is there a type for any number? To choose types for real data, we need to know what each one is for.


Learning Objectives

By the end of this lecture you will be able to:

  • Explain what a data type is and why Dart needs one for every value
  • Use int for whole numbers and double for decimal numbers
  • Use num when a value may be either kind of number
  • Use String for text and bool for yes/no values
  • Recognize that the type decides which operations are allowed
  • Name Dart's collection types List, Set, and Map, and the special value null
  • Choose the right type for a piece of real-world data
  • Recognize the error messages Dart produces when a value has the wrong type

1. What Is a Data Type?

A data type describes a kind of value: whole number, decimal, text, or yes/no. Every value in Dart has a type, and every variable is fixed to one.

Analogy — think of a printed form. The "Age" box expects a number, the "Name" box letters, and the "Member?" box a tick. Writing "Sara" in the Age box makes no sense. Data types are those expectations, and Dart checks them for you.


2. The Five Basic Types at a Glance

TypeHoldsExample values
intwhole numbers20, -5, 0
doubledecimal numbers99.5, -3.2, 25.0
numeither an int or a double20, 99.5
Stringtext'Ali', 'Hello!', '42'
boola yes/no answertrue, false
dart
void main() {
  int age = 20;
  double height = 5.8;
  num weight = 62;
  String name = 'Zayan';
  bool isStudent = true;
  print('$name, $age, $height, $weight, $isStudent');
}
output
Zayan, 20, 5.8, 62, true

3. int — Whole Numbers

An int holds a whole number with no decimal part. It can be positive, negative, or zero.

dart
void main() {
  int students = 30;
  int goalDifference = -4;
  print(students);
  print(goalDifference);
}
output
30
-4

Use int for things you count: people, items, marks.


4. double — Decimal Numbers

A double holds a number with a decimal part, and always prints one — even .0.

dart
void main() {
  double price = 99.99;
  double distance = 10;
  print(price);
  print(distance);
}
output
99.99
10.0

Use double for things you measure: prices, weights, temperatures, distances.


5. int and double Are Different Types

Both are numbers, but Dart keeps them apart. An int variable cannot hold a decimal:

dart
void main() {
  int age = 20.5;
  print(age);
}
output
Error: A value of type 'double' can't be assigned to a variable of type 'int'.
  int age = 20.5;
            ^

The other direction has a catch. A whole number written directly into a double is fine (double distance = 10;), but an int variable is not:

dart
void main() {
  int count = 5;
  double price = count;
  print(price);
}
output
Error: A value of type 'int' can't be assigned to a variable of type 'double'.
  double price = count;
                 ^

Turning an int into a double (and back) is called conversion — we will study it in Lecture 010.


6. num — Either Kind of Number

num is the general number type. Both int and double are nums, so a num variable accepts either:

dart
void main() {
  num price = 10;
  print(price);
  price = 10.5;
  print(price);
}
output
10
10.5

10 prints without .0 because the value inside is still the int 10. Use num only when a value really can be either kind. Numbers get a full lecture in Lecture 009.


7. String — Text

A String holds text inside quotes. Single and double quotes both work.

dart
void main() {
  String name = 'Sara';
  String city = "Lahore";
  String message = "It's raining";
  print(name);
  print(city);
  print(message);
}
output
Sara
Lahore
It's raining

Double quotes are handy when the text contains an apostrophe. Anything in quotes is text — even digits. '20' is a String, not a number:

dart
void main() {
  String age = 20;
  print(age);
}
output
Error: A value of type 'int' can't be assigned to a variable of type 'String'.
  String age = 20;
               ^

Strings have many more features; the next lecture is all about them.


8. bool — True or False

A bool (short for Boolean) holds exactly one of two values: true or false. Give it a yes/no name such as hasPaid or isOpen.

dart
void main() {
  bool hasPaid = true;
  bool isOpen = false;
  print('Paid: $hasPaid');
  print('Open: $isOpen');
}
output
Paid: true
Open: false

true and false are written without quotes. Some languages treat 1 as true; Dart does not:

dart
void main() {
  bool isOpen = 1;
  print(isOpen);
}
output
Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  bool isOpen = 1;
                ^

A bool usually comes from a comparison such as "is the mark at least 50?" — comparisons arrive in Lecture 018, and decisions with if in Lecture 022.


9. The Type Decides What You Can Do

A type decides which operations make sense. + adds two numbers, but joins two strings:

dart
void main() {
  print(20 + 5);
  print('Ali' + ' Khan');
}
output
25
Ali Khan

Mixing them is rejected:

dart
void main() {
  print(20 + ' years');
}
output
Error: A value of type 'String' can't be assigned to a variable of type 'num'.
  print(20 + ' years');
             ^

Adding to a number needs another number (num). To mix text and numbers, use interpolation: '$age years'.


10. Collections and null — A First Look

Dart also has three collection types for holding many values, plus a special value meaning "nothing":

TypeHoldsExample
Listan ordered list of values[80, 75, 85]
Setvalues with no duplicates{'Ali', 'Sara'}
Mappairs of key → value{'Ali': 80, 'Sara': 92}
Nullthe single value null, "no value"null
dart
void main() {
  var marks = [80, 75, 85];
  var names = {'Ali', 'Sara'};
  var scores = {'Ali': 80, 'Sara': 92};
  print(marks);
  print(names);
  print(scores);
}
output
[80, 75, 85]
{Ali, Sara}
{Ali: 80, Sara: 92}

For now, just recognize the names. Lists start in Lecture 039, Sets in Lecture 047, and Maps in Lecture 049. null — and why an int cannot hold it — is the subject of Null Safety, starting in Lecture 012.


11. Choosing the Right Type

Ask what the value is and what you will do with it:

DataTypeWhy
number of itemsintcounted, never fractional
price, weight, temperaturedoublemeasured, may have decimals
name, city, emailStringtext
phone number, ID card numberStringmade of digits, but never calculated with
member or not, paid or notboolonly two possible answers

The phone number is the classic trap: 03001234567 looks like a number, but you never add two phone numbers, and the leading 0 matters. Store it as text.


12. A Real-World Example

dart
void main() {
  const double taxRate = 0.05;

  String customer = 'Sara';
  String phone = '03001234567';
  int quantity = 3;
  double price = 450.0;
  bool isMember = true;

  double subtotal = price * quantity;
  double tax = subtotal * taxRate;
  double total = subtotal + tax;

  print('Customer: $customer ($phone)');
  print('Member: $isMember');
  print('Items: $quantity');
  print('Total: $total');
}
output
Customer: Sara (03001234567)
Member: true
Items: 3
Total: 1417.5

Every value has the type that matches what it is: text, a count, money, or a yes/no. taxRate is const because it never changes (Lecture 006).


Code Examples

Example 1 — Whole Numbers

dart
void main() {
  int apples = 12;
  int floor = -2;
  print(apples);
  print(floor);
}
output
12
-2

Example 2 — Decimal Numbers

dart
void main() {
  double weight = 62.5;
  double height = 6;
  print(weight);
  print(height);
}
output
62.5
6.0

Example 3 — A num Holding Both Kinds

dart
void main() {
  num reading = 7;
  print(reading);
  reading = 7.25;
  print(reading);
}
output
7
7.25

Example 4 — Text in Both Quote Styles

dart
void main() {
  String first = 'Zayan';
  String greeting = "Zayan's notebook";
  print(first);
  print(greeting);
}
output
Zayan
Zayan's notebook

Example 5 — Yes/No Values

dart
void main() {
  bool isRaining = false;
  bool hasTicket = true;
  print('Raining: $isRaining');
  print('Ticket: $hasTicket');
}
output
Raining: false
Ticket: true

Example 6 — Same +, Different Types

dart
void main() {
  int a = 10;
  int b = 20;
  String c = '10';
  String d = '20';
  print(a + b);
  print(c + d);
}
output
30
1020

How it works

  1. a and b are int, so + adds them: 30.
  2. c and d are String, so + joins them side by side: '1020'.
  3. The digits look the same; the type decides the result.

Example 7 — Student Record

dart
void main() {
  String student = 'Ali';
  int marks = 412;
  double percentage = 82.4;
  bool passed = true;
  print('Student: $student');
  print('Marks: $marks');
  print('Percentage: $percentage');
  print('Passed: $passed');
}
output
Student: Ali
Marks: 412
Percentage: 82.4
Passed: true

How it works

  1. Text is a String; counted marks are an int; the percentage may have decimals, so it is a double.
  2. "Passed" has only two possible answers, so it is a bool.

Example 8 — Shopping Total

dart
void main() {
  String product = 'Notebook';
  int quantity = 4;
  double price = 120.5;
  double total = price * quantity;
  print('$quantity x $product');
  print('Total: $total');
}
output
4 x Notebook
Total: 482.0

How it works

  1. double times int gives a double, so total must be a double.
  2. 120.5 * 4 is 482.0, printed with its decimal part.

Example 9 — Bank Balance

dart
void main() {
  String owner = 'Sara';
  String accountNumber = '00451239';
  double balance = 2500.0;
  bool isActive = true;
  balance = balance - 750.25;
  print('Owner: $owner');
  print('Account: $accountNumber');
  print('Active: $isActive');
  print('Balance: $balance');
}
output
Owner: Sara
Account: 00451239
Active: true
Balance: 1749.75

How it works

  1. accountNumber is a String: it is never calculated with, and its leading zeros matter.
  2. balance is a double because money can have cents, so 1749.75 fits.

Example 10 — Temperature Reading

dart
void main() {
  String city = 'Karachi';
  int day = 3;
  num celsius = 31;
  bool isHot = true;
  print('Day $day in $city: $celsius°C');
  celsius = 29.5;
  print('Evening: $celsius°C');
  print('Hot day: $isHot');
}
output
Day 3 in Karachi: 31°C
Evening: 29.5°C
Hot day: true

How it works

  1. celsius starts as the whole number 31 and later becomes 29.5. A num accepts both.
  2. 31 prints without .0 because the value inside the num is an int.

Common Beginner Mistakes

1. Putting a decimal into an int

dart
void main() {
  // int age = 20.5; // ✗
}
output
Error: A value of type 'double' can't be assigned to a variable of type 'int'.

Use double when a value may have a decimal part.

2. Copying an int variable into a double

dart
void main() {
  int count = 5;
  // double price = count; // ✗
}
output
Error: A value of type 'int' can't be assigned to a variable of type 'double'.

A whole number written directly is fine; an int variable is not. See Lecture 010.

3. Storing a number where text is expected

dart
void main() {
  // String age = 20; // ✗
}
output
Error: A value of type 'int' can't be assigned to a variable of type 'String'.

Either the variable should be an int, or the value should be text: '20'.

4. Writing true in quotes, or using 1

dart
void main() {
  // bool isOpen = 'true'; // ✗
  // bool isOpen = 1; // ✗
}
output
Error: A value of type 'String' can't be assigned to a variable of type 'bool'.
Error: A value of type 'int' can't be assigned to a variable of type 'bool'.

A bool accepts only true or false, with no quotes.

5. Adding text to a number

dart
void main() {
  // print(20 + ' years'); // ✗
}
output
Error: A value of type 'String' can't be assigned to a variable of type 'num'.

Use interpolation instead: print('20 years'); or print('$age years');.

6. Storing a phone number as an int

You never calculate with a phone number, and an int drops its leading 0. Store phone, ID, and account numbers as String.


Best Practices

  • Choose the type from what the value is and what you will do with it.
  • Use int for counts and double for measurements and money.
  • Use num only when a value genuinely can be either kind of number.
  • Store digits you never calculate with — phone numbers, IDs — as String.
  • Give bool variables yes/no names: isMember, hasPaid, isOpen.
  • Read type errors carefully — they name both the type you gave and the type expected.

Mini Challenge

Store a library book's details: title 'Dart Basics', number of pages 320, price 850.5, and whether it is available (true). Choose the right type for each and print them.

Your program should print exactly:

output
Title: Dart Basics
Pages: 320
Price: 850.5
Available: true

Try it yourself before opening the solution.

Show solution
dart
void main() {
  String title = 'Dart Basics';
  int pages = 320;
  double price = 850.5;
  bool isAvailable = true;
  print('Title: $title');
  print('Pages: $pages');
  print('Price: $price');
  print('Available: $isAvailable');
}
output
Title: Dart Basics
Pages: 320
Price: 850.5
Available: true

How it works

  1. The title is text (String), pages are counted (int), and the price has a decimal part (double).
  2. Availability is yes or no, so it is a bool.

Practice Questions

Work through these in order — they run from beginner to application to challenge. Every question gives you exact values, so you can compare your output with the answers.

Beginner

  1. Create an int named chairs holding 24 and a double named width holding 1.5. Print both, each on its own line.
  2. Create a String named country holding 'Pakistan' and a bool named isSunny holding true. Print both, each on its own line.
  3. Create a num named score holding 40, print it, change it to 40.5, and print it again.

Application

  1. Choose a type for each: an age in years, a bag's weight in kilograms, an email address, whether a light is on. Store age = 25, weight = 3.2, email = 'ali@mail.com', and isOn = false with those types and print all four on one line, separated by commas.

  2. Create String x = '5'; and String y = '7'; and print x + y. Then create int p = 5; and int q = 7; and print p + q. Predict both outputs before running.

  3. This program does not compile. Change the type of one variable so it prints 72.5:

    dart
    void main() {
      int weight = 72.5;
      print(weight);
    }
  4. Store Zayan's ID card number '4210112345678' and his age 6 using suitable types. Print ID: 4210112345678 and Age: 6.

  5. Store a product name 'Pen', quantity 10, and price 15.5. Calculate the total and print Pen total: 155.0.

Challenge

  1. A student has marks 78, 85, and 91 out of 300. Store the marks as int, calculate the total, then the percentage (total * 100 / 300), and store whether the student passed as bool passed = true;. Print Total: 254, Percentage: 84.66666666666667, and Passed: true.

  2. This program does not compile. Fix it without changing any variable's type, so that it prints Ali is 20 years old:

    dart
    void main() {
      String name = 'Ali';
      int age = 20;
      print(name + ' is ' + age + ' years old');
    }

Practice Answers

Question 1 — Solution

dart
void main() {
  int chairs = 24;
  double width = 1.5;
  print(chairs);
  print(width);
}
output
24
1.5

Question 2 — Solution

dart
void main() {
  String country = 'Pakistan';
  bool isSunny = true;
  print(country);
  print(isSunny);
}
output
Pakistan
true

Question 3 — Solution

dart
void main() {
  num score = 40;
  print(score);
  score = 40.5;
  print(score);
}
output
40
40.5

Question 4 — Solution

Age is counted (int), weight is measured (double), an email is text (String), and on/off is yes/no (bool).

dart
void main() {
  int age = 25;
  double weight = 3.2;
  String email = 'ali@mail.com';
  bool isOn = false;
  print('$age, $weight, $email, $isOn');
}
output
25, 3.2, ali@mail.com, false

Question 5 — Solution

dart
void main() {
  String x = '5';
  String y = '7';
  print(x + y);
  int p = 5;
  int q = 7;
  print(p + q);
}
output
57
12

For strings, + joins '5' and '7' into '57'. For integers, it adds them to 12.

Question 6 — Solution

dart
void main() {
  double weight = 72.5;
  print(weight);
}
output
72.5

Question 7 — Solution

dart
void main() {
  String idNumber = '4210112345678';
  int age = 6;
  print('ID: $idNumber');
  print('Age: $age');
}
output
ID: 4210112345678
Age: 6

The ID card number is a String because it is never calculated with.

Question 8 — Solution

dart
void main() {
  String product = 'Pen';
  int quantity = 10;
  double price = 15.5;
  double total = price * quantity;
  print('$product total: $total');
}
output
Pen total: 155.0

Question 9 — Solution

dart
void main() {
  int math = 78;
  int science = 85;
  int english = 91;
  int total = math + science + english;
  double percentage = total * 100 / 300;
  bool passed = true;
  print('Total: $total');
  print('Percentage: $percentage');
  print('Passed: $passed');
}
output
Total: 254
Percentage: 84.66666666666667
Passed: true

Division with / always gives a double, so percentage must be a double. Rounding long decimals is covered in Lecture 009.

Question 10 — Solution

dart
void main() {
  String name = 'Ali';
  int age = 20;
  print('$name is $age years old');
}
output
Ali is 20 years old

The original adds an int to a String, which Dart rejects. Interpolation puts age into the text without changing any type.


Quick Revision

  1. What is a data type?
  2. Which type holds whole numbers, and which holds decimals?
  3. Why does double distance = 10; print 10.0?
  4. What kind of value can a num variable hold?
  5. Is '42' a number or text?
  6. What are the only two bool values?
  7. What does + do with two strings?
  8. Why should a phone number be a String?

Key Takeaways

  • A data type describes what kind of value something is, and what you can do with it.
  • int holds whole numbers; double holds decimal numbers and always prints a decimal part.
  • int and double are different types; copying one into the other needs conversion.
  • num accepts both int and double; use it only when either is possible.
  • String holds text in single or double quotes — digits in quotes are text.
  • bool holds only true or false, without quotes.
  • The type decides the operation: + adds numbers but joins strings.
  • List, Set, Map, and null exist too — each gets its own lectures later.

What's Next?

Lecture 08 — Strings. Text is everywhere: names, messages, receipts. Now that you know String is Dart's type for text, the next lecture explores it properly — quotes and special characters, multi-line text, interpolation with expressions, and everyday string tools.


Video lecture

Subscribe
Placeholder video — the real recording isn't linked yet.