Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Basics of Dart  ›  Lesson

Built-in Types

Built-in Types 12 minDart's core types are int, double, String and bool for single values plus List, Set and Map for collections, and strings can embed values with interpolation.

Built-in Types

Every piece of Sprout's data fits into a handful of types that ship with Dart. Knowing which one to reach for, and what each one does at the edges, saves you from bugs like a watering interval of 3.5 days or a lookup that silently returns nothing.

The rule

Numbers, text, truth, and three collections: List, Set, Map.

Single values

int days = 3;            // whole numbers
double litres = 0.25;    // numbers with a fractional part
String name = 'Fern';    // text, single or double quotes
bool outdoors = false;   // true or false

int and double are both kinds of num. Strings can embed values with interpolation: '$name needs ${litres * 1000} ml' gives Fern needs 250.0 ml. Use $name for a plain variable and ${...} for any expression.

Collections

List<String> order = ['Fern', 'Cactus', 'Fern'];   // ordered, duplicates allowed
Set<String> kinds = {'Fern', 'Cactus'};            // no duplicates
Map<String, int> every = {'Fern': 3, 'Cactus': 14}; // key to value
  • A List keeps position: order[0] is 'Fern'.
  • A Set keeps each value once: order.toSet().length is 2.
  • A Map looks up by key: every['Fern'] is 3, and every['Palm'] is null, which is why its type is int?.

The shortcut

Pick the collection by the question you will ask it most.

Choosing a collection for SproutStep 1 of 4
  1. "What comes next?"

    The watering route through the flat has an order. That is a List.

The trap to watch is the Map lookup. Reading a missing key does not crash; it quietly gives null. Null safety forces you to handle that case before you can use the number.

Drills

Answer fast — trust the rule.

quick drill

What does '${2 + 3} pots' produce?

quick drill

What is ['Fern', 'Cactus', 'Fern'].toSet().length?

quick drill

Given Map<String, int> every = {'Fern': 3};, what is every['Palm']?

quick drill

Which type fits "0.5 litres of water"?

quick drill

Sprout needs the plants in the order you walk past them, and the same plant may appear twice. Which type?

Remember

  • Numbers, text, truth, and three collections: List, Set, Map.
  • Choose the collection by its main question: next? has? value for?
  • Trap: a missing Map key gives null, and $ only reads a bare name; use ${} for expressions.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainModel Sprout's data

Describe how you would store these in Dart and why: a plant's name, how many days between waterings, how much water in litres, whether it is outdoors, the list of plants in watering order, and a lookup from plant name to interval.