On this page14 sections
Lecture 007 — Data Types
| Category | 3 — Variables & Constants |
| Lecture | 007 of 057 |
| Estimated video length | 14 min |
| Target | Dart 3.12 |
| Level | Beginner |
Every value in Dart has a type: whole numbers, decimals, text, true/false, collections. We meet the built-in types and learn how to check a value's type.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lecture 006: declaring variables with a type or var.
Learning Objectives
- Use
int,double,num,Stringandbool - Recognise
ListandMap - Know what
dynamicandObjectare, and why to avoiddynamic - Check a type with
isandruntimeType - Convert between types
1. Numbers: int, double and num
int students = 30; // whole numbers
double price = 4.5; // numbers with a decimal point
num anything = 7; // either an int or a double
anything = 7.5; // still finedouble d = 3; // fine: Dart stores 3.0
// int i = 3.5; // error: a double isn't an int2. Text: String
String greeting = 'Hello';
String quote = "It's Day 3"; // double quotes when the text has an apostropheStrings get their own lecture — Lecture 009.
3. True or False: bool
bool isOpen = true;
bool hasPaid = false;Only true and false — Dart never treats 0 or '' as false.
4. Collections: List and Map
List<String> fruits = ['apple', 'mango', 'banana'];
Map<String, int> ages = {'Ali': 21, 'Sara': 19};A List is an ordered row of values; a Map pairs keys with values. Day 5 covers both.
5. dynamic and Object
dynamic box = 5;
box = 'now text'; // allowed: dynamic switches off type checking
Object thing = 5;
thing = 'text'; // also allowed: everything is an ObjectThe difference: with dynamic, Dart lets you call anything on it and only fails when the
program runs. With Object, Dart checks first. Prefer a real type, then Object, and
dynamic only when you truly must.
6. Checking a Type
var value = 42;
print(value is int); // true
print(value is String); // false
print(value.runtimeType); // int7. Converting Between Types
| From → to | Code | Result |
|---|---|---|
| text → int | int.parse('21') | 21 |
| text → double | double.parse('3.5') | 3.5 |
| number → text | 21.toString() | '21' |
| double → int | 3.9.toInt() | 3 (cut, not rounded) |
| double → rounded int | 3.9.round() | 4 |
| int → double | 5.toDouble() | 5.0 |
Code Examples
Example 1 — A Product Record
void main() {
String product = 'Notebook';
int stock = 25;
double price = 120.5;
bool onSale = true;
List<String> colors = ['red', 'blue'];
Map<String, int> sold = {'Monday': 4, 'Tuesday': 7};
print('$product: $stock in stock at $price');
print('On sale: $onSale');
print('Colors: $colors');
print('Sold: $sold');
var total = stock * price;
print('Stock value: $total (${total.runtimeType})');
print('Rounded: ${total.round()}');
}Output
Notebook: 25 in stock at 120.5
On sale: true
Colors: [red, blue]
Sold: {Monday: 4, Tuesday: 7}
Stock value: 3012.5 (double)
Rounded: 3013An int times a double gives a double.
Common Beginner Mistakes
1. Putting a decimal in an int — int price = 4.5; is an error. Use double.
2. Numbers in quotes — '25' is a String, not a number. '25' + 1 doesn't work; use
int.parse('25') + 1.
3. Reaching for dynamic — it hides mistakes until the program crashes. Use real types.
Practice Questions
- Which type holds
3.14? Which holds3? - Can a
numvariable hold both? Can anint? - What does
print(7.8.toInt());print? - How do you check whether
xis aString?
Practice Answers
double;int.- Yes; no — an
intcan't hold adouble. 7.x is String.
Key Takeaways
int,double(andnumfor either),String,bool,List,Map.dynamicturns off type checks — avoid it;Objectkeeps them.istests a type;runtimeTypenames it.- Convert with
int.parse,toString,toInt,round,toDouble.