On this page24 sections
- Prerequisites
- Connection With Previous Lectures
- Learning Objectives
- 1. What Is a Data Type?
- 2. The Five Basic Types at a Glance
- 3. int — Whole Numbers
- 4. double — Decimal Numbers
- 5. int and double Are Different Types
- 6. num — Either Kind of Number
- 7. String — Text
- 8. bool — True or False
- 9. The Type Decides What You Can Do
- 10. Collections and null — A First Look
- 11. Choosing the Right Type
- 12. A Real-World Example
- Code Examples
- Common Beginner Mistakes
- Best Practices
- Mini Challenge
- Practice Questions
- Practice Answers
- Quick Revision
- Key Takeaways
- What's Next?
Lecture 07 — Dart Data Types
| Category | 2 — Variables & Data Types |
| Lecture | 007 of 140 |
| Estimated video length | 12 min |
| Target | Dart 3.12.2 |
| Level | Beginner |
We have used
String,int,double, andboolwithout 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
varand type inferencefinalandconstfor values that must not change
Connection With Previous Lectures
Since Lecture 004 we have written lines like these:
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
intfor whole numbers anddoublefor decimal numbers - Use
numwhen a value may be either kind of number - Use
Stringfor text andboolfor yes/no values - Recognize that the type decides which operations are allowed
- Name Dart's collection types
List,Set, andMap, and the special valuenull - 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
| Type | Holds | Example values |
|---|---|---|
int | whole numbers | 20, -5, 0 |
double | decimal numbers | 99.5, -3.2, 25.0 |
num | either an int or a double | 20, 99.5 |
String | text | 'Ali', 'Hello!', '42' |
bool | a yes/no answer | true, false |
void main() {
int age = 20;
double height = 5.8;
num weight = 62;
String name = 'Zayan';
bool isStudent = true;
print('$name, $age, $height, $weight, $isStudent');
}Zayan, 20, 5.8, 62, true3. int — Whole Numbers
An int holds a whole number with no decimal part. It can be positive, negative, or zero.
void main() {
int students = 30;
int goalDifference = -4;
print(students);
print(goalDifference);
}30
-4Use 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.
void main() {
double price = 99.99;
double distance = 10;
print(price);
print(distance);
}99.99
10.0Use 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:
void main() {
int age = 20.5;
print(age);
}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:
void main() {
int count = 5;
double price = count;
print(price);
}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:
void main() {
num price = 10;
print(price);
price = 10.5;
print(price);
}10
10.510 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.
void main() {
String name = 'Sara';
String city = "Lahore";
String message = "It's raining";
print(name);
print(city);
print(message);
}Sara
Lahore
It's rainingDouble quotes are handy when the text contains an apostrophe. Anything in quotes is text —
even digits. '20' is a String, not a number:
void main() {
String age = 20;
print(age);
}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.
void main() {
bool hasPaid = true;
bool isOpen = false;
print('Paid: $hasPaid');
print('Open: $isOpen');
}Paid: true
Open: falsetrue and false are written without quotes. Some languages treat 1 as true; Dart does
not:
void main() {
bool isOpen = 1;
print(isOpen);
}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:
void main() {
print(20 + 5);
print('Ali' + ' Khan');
}25
Ali KhanMixing them is rejected:
void main() {
print(20 + ' years');
}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":
| Type | Holds | Example |
|---|---|---|
List | an ordered list of values | [80, 75, 85] |
Set | values with no duplicates | {'Ali', 'Sara'} |
Map | pairs of key → value | {'Ali': 80, 'Sara': 92} |
Null | the single value null, "no value" | null |
void main() {
var marks = [80, 75, 85];
var names = {'Ali', 'Sara'};
var scores = {'Ali': 80, 'Sara': 92};
print(marks);
print(names);
print(scores);
}[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:
| Data | Type | Why |
|---|---|---|
| number of items | int | counted, never fractional |
| price, weight, temperature | double | measured, may have decimals |
| name, city, email | String | text |
| phone number, ID card number | String | made of digits, but never calculated with |
| member or not, paid or not | bool | only 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
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');
}Customer: Sara (03001234567)
Member: true
Items: 3
Total: 1417.5Every 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
void main() {
int apples = 12;
int floor = -2;
print(apples);
print(floor);
}12
-2Example 2 — Decimal Numbers
void main() {
double weight = 62.5;
double height = 6;
print(weight);
print(height);
}62.5
6.0Example 3 — A num Holding Both Kinds
void main() {
num reading = 7;
print(reading);
reading = 7.25;
print(reading);
}7
7.25Example 4 — Text in Both Quote Styles
void main() {
String first = 'Zayan';
String greeting = "Zayan's notebook";
print(first);
print(greeting);
}Zayan
Zayan's notebookExample 5 — Yes/No Values
void main() {
bool isRaining = false;
bool hasTicket = true;
print('Raining: $isRaining');
print('Ticket: $hasTicket');
}Raining: false
Ticket: trueExample 6 — Same +, Different Types
void main() {
int a = 10;
int b = 20;
String c = '10';
String d = '20';
print(a + b);
print(c + d);
}30
1020How it works
aandbareint, so+adds them:30.canddareString, so+joins them side by side:'1020'.- The digits look the same; the type decides the result.
Example 7 — Student Record
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');
}Student: Ali
Marks: 412
Percentage: 82.4
Passed: trueHow it works
- Text is a
String; counted marks are anint; the percentage may have decimals, so it is adouble. - "Passed" has only two possible answers, so it is a
bool.
Example 8 — Shopping Total
void main() {
String product = 'Notebook';
int quantity = 4;
double price = 120.5;
double total = price * quantity;
print('$quantity x $product');
print('Total: $total');
}4 x Notebook
Total: 482.0How it works
doubletimesintgives adouble, sototalmust be adouble.120.5 * 4is482.0, printed with its decimal part.
Example 9 — Bank Balance
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');
}Owner: Sara
Account: 00451239
Active: true
Balance: 1749.75How it works
accountNumberis aString: it is never calculated with, and its leading zeros matter.balanceis adoublebecause money can have cents, so1749.75fits.
Example 10 — Temperature Reading
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');
}Day 3 in Karachi: 31°C
Evening: 29.5°C
Hot day: trueHow it works
celsiusstarts as the whole number31and later becomes29.5. Anumaccepts both.31prints without.0because the value inside thenumis anint.
Common Beginner Mistakes
1. Putting a decimal into an int
void main() {
// int age = 20.5; // ✗
}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
void main() {
int count = 5;
// double price = count; // ✗
}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
void main() {
// String age = 20; // ✗
}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
void main() {
// bool isOpen = 'true'; // ✗
// bool isOpen = 1; // ✗
}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
void main() {
// print(20 + ' years'); // ✗
}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
intfor counts anddoublefor measurements and money. - Use
numonly when a value genuinely can be either kind of number. - Store digits you never calculate with — phone numbers, IDs — as
String. - Give
boolvariables 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:
Title: Dart Basics
Pages: 320
Price: 850.5
Available: trueTry it yourself before opening the solution.
Show solution
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');
}Title: Dart Basics
Pages: 320
Price: 850.5
Available: trueHow it works
- The title is text (
String), pages are counted (int), and the price has a decimal part (double). - 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
- Create an
intnamedchairsholding24and adoublenamedwidthholding1.5. Print both, each on its own line. - Create a
Stringnamedcountryholding'Pakistan'and aboolnamedisSunnyholdingtrue. Print both, each on its own line. - Create a
numnamedscoreholding40, print it, change it to40.5, and print it again.
Application
-
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', andisOn = falsewith those types and print all four on one line, separated by commas. -
Create
String x = '5';andString y = '7';and printx + y. Then createint p = 5;andint q = 7;and printp + q. Predict both outputs before running. -
This program does not compile. Change the type of one variable so it prints
72.5:void main() { int weight = 72.5; print(weight); } -
Store Zayan's ID card number
'4210112345678'and his age6using suitable types. PrintID: 4210112345678andAge: 6. -
Store a product name
'Pen', quantity10, and price15.5. Calculate the total and printPen total: 155.0.
Challenge
-
A student has marks
78,85, and91out of300. Store the marks asint, calculate the total, then the percentage (total * 100 / 300), and store whether the student passed asbool passed = true;. PrintTotal: 254,Percentage: 84.66666666666667, andPassed: true. -
This program does not compile. Fix it without changing any variable's type, so that it prints
Ali is 20 years old:void main() { String name = 'Ali'; int age = 20; print(name + ' is ' + age + ' years old'); }
Practice Answers
Question 1 — Solution
void main() {
int chairs = 24;
double width = 1.5;
print(chairs);
print(width);
}24
1.5Question 2 — Solution
void main() {
String country = 'Pakistan';
bool isSunny = true;
print(country);
print(isSunny);
}Pakistan
trueQuestion 3 — Solution
void main() {
num score = 40;
print(score);
score = 40.5;
print(score);
}40
40.5Question 4 — Solution
Age is counted (int), weight is measured (double), an email is text (String), and on/off
is yes/no (bool).
void main() {
int age = 25;
double weight = 3.2;
String email = 'ali@mail.com';
bool isOn = false;
print('$age, $weight, $email, $isOn');
}25, 3.2, ali@mail.com, falseQuestion 5 — Solution
void main() {
String x = '5';
String y = '7';
print(x + y);
int p = 5;
int q = 7;
print(p + q);
}57
12For strings, + joins '5' and '7' into '57'. For integers, it adds them to 12.
Question 6 — Solution
void main() {
double weight = 72.5;
print(weight);
}72.5Question 7 — Solution
void main() {
String idNumber = '4210112345678';
int age = 6;
print('ID: $idNumber');
print('Age: $age');
}ID: 4210112345678
Age: 6The ID card number is a String because it is never calculated with.
Question 8 — Solution
void main() {
String product = 'Pen';
int quantity = 10;
double price = 15.5;
double total = price * quantity;
print('$product total: $total');
}Pen total: 155.0Question 9 — Solution
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');
}Total: 254
Percentage: 84.66666666666667
Passed: trueDivision with / always gives a double, so percentage must be a double. Rounding long
decimals is covered in Lecture 009.
Question 10 — Solution
void main() {
String name = 'Ali';
int age = 20;
print('$name is $age years old');
}Ali is 20 years oldThe original adds an int to a String, which Dart rejects. Interpolation puts age into the
text without changing any type.
Quick Revision
- What is a data type?
- Which type holds whole numbers, and which holds decimals?
- Why does
double distance = 10;print10.0? - What kind of value can a
numvariable hold? - Is
'42'a number or text? - What are the only two
boolvalues? - What does
+do with two strings? - 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.
intholds whole numbers;doubleholds decimal numbers and always prints a decimal part.intanddoubleare different types; copying one into the other needs conversion.numaccepts bothintanddouble; use it only when either is possible.Stringholds text in single or double quotes — digits in quotes are text.boolholds onlytrueorfalse, without quotes.- The type decides the operation:
+adds numbers but joins strings. List,Set,Map, andnullexist 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.