On this page14 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Numbers: int, double and num
  4. 2. Text: String
  5. 3. True or False: bool
  6. 4. Collections: List and Map
  7. 5. dynamic and Object
  8. 6. Checking a Type
  9. 7. Converting Between Types
  10. Code Examples
  11. Common Beginner Mistakes
  12. Practice Questions
  13. Practice Answers
  14. Key Takeaways

Lecture 007 — Data Types

Category3 — Variables & Constants
Lecture007 of 057
Estimated video length14 min
TargetDart 3.12
LevelBeginner

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, String and bool
  • Recognise List and Map
  • Know what dynamic and Object are, and why to avoid dynamic
  • Check a type with is and runtimeType
  • Convert between types

1. Numbers: int, double and num

dart
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 fine
dart
double d = 3;   // fine: Dart stores 3.0
// int i = 3.5; // error: a double isn't an int

2. Text: String

dart
String greeting = 'Hello';
String quote = "It's Day 3";   // double quotes when the text has an apostrophe

Strings get their own lecture — Lecture 009.

3. True or False: bool

dart
bool isOpen = true;
bool hasPaid = false;

Only true and false — Dart never treats 0 or '' as false.

4. Collections: List and Map

dart
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

dart
dynamic box = 5;
box = 'now text';   // allowed: dynamic switches off type checking

Object thing = 5;
thing = 'text';     // also allowed: everything is an Object

The 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

dart
var value = 42;
print(value is int);        // true
print(value is String);     // false
print(value.runtimeType);   // int

7. Converting Between Types

From → toCodeResult
text → intint.parse('21')21
text → doubledouble.parse('3.5')3.5
number → text21.toString()'21'
double → int3.9.toInt()3 (cut, not rounded)
double → rounded int3.9.round()4
int → double5.toDouble()5.0

Code Examples

Example 1 — A Product Record

dart
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

code
Notebook: 25 in stock at 120.5
On sale: true
Colors: [red, blue]
Sold: {Monday: 4, Tuesday: 7}
Stock value: 3012.5 (double)
Rounded: 3013

An 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

  1. Which type holds 3.14? Which holds 3?
  2. Can a num variable hold both? Can an int?
  3. What does print(7.8.toInt()); print?
  4. How do you check whether x is a String?

Practice Answers

  1. double; int.
  2. Yes; no — an int can't hold a double.
  3. 7.
  4. x is String.

Key Takeaways

  • int, double (and num for either), String, bool, List, Map.
  • dynamic turns off type checks — avoid it; Object keeps them.
  • is tests a type; runtimeType names it.
  • Convert with int.parse, toString, toInt, round, toDouble.