Thuta Learning
Dart
BasicProgrammingBeginner

Dart HOME

DartLesson 115 min

What you'll walk away with

  • Describe in a few sentences what Dart is: a typed, null-safe language from Google that Flutter apps are written in
  • Read a small Dart program and name its parts: a typed function, main(), const and var, and string interpolation
  • Explain why Dart's type checker rejects some JavaScript-style code before the program ever runs
  • Know the five chapters of the course and the order to study them

Explain it simply

Dart in plain words

Dart is a programming language made by Google. If you have ever used a Flutter app on a phone, most of what you were looking at was written in Dart: the screens, the buttons and the code that decides what happens when you tap them. Dart is also used for command-line tools and servers, so it is a general-purpose language and not only a helper for Flutter.

Think of Dart as a careful friend who reads your code before it runs. You write down what kind of thing each value is (text, a whole number, yes or no), and Dart checks that you keep those promises. This course starts from zero and teaches that friend's habits one lesson at a time.

Problem → Why → Solution

The problem: JavaScript habits do not carry over unchanged

Many people arrive at this course from JavaScript or Python, with a Flutter tutorial open in another tab. They type what feels natural: a function keyword, let for a variable, console.log for output, and no semicolon at the end of the line. Dart refuses to run it and prints several errors at once, which feels as if the language is being difficult. The first error it prints is hello.dart:2:1: Error: Type 'function' not found.

None of it is arbitrary. Dart is a different language with its own spelling for the same ideas, and it checks much more before it lets a program start. Meeting those first errors on purpose, with a program you control, is far less stressful than meeting them halfway through a Flutter project.

hello.dart
// JavaScript habits typed into a .dart file
function main() {
  let name = "Aye";
  console.log("Hi " + name)
}

Why Dart is strict

Dart compiles your code and checks every name, type and semicolon before the first line runs. A mistake then costs a few seconds at the keyboard instead of a crash in front of a user. The price is that you must learn Dart's own spelling: void main(), typed parameters and statements that end with a semicolon.

Core concept: a language, a toolchain and a set of promises

Dart is three things that work together. The first is the language: it is statically typed, so the compiler knows the type of every value; it is null-safe, so a variable that may hold null must say so in its type; and it is object-oriented, so nearly everything, even the number 5, is an object with methods. The second is a family of compilers and runtimes. While you develop, the Dart VM runs your code with just-in-time (JIT) compilation, which is what makes quick restarts and Flutter's hot reload possible. For release, Dart can compile ahead of time (AOT) into native machine code, or translate your program to JavaScript or WebAssembly for the web. The third is the toolchain: a single dart command that runs, analyzes, formats, tests and packages your code.

PartWhat it gives you
The languageStatic types, sound null safety, classes, async/await, and records and patterns in Dart 3
Compilers and runtimesA VM with JIT for development, AOT native code for release, JavaScript and WebAssembly for browsers
The dart commandrun, analyze, format, test, compile and pub for packages, all from one tool

All of that sits behind one simple daily loop. You write a .dart file, Dart checks it, and only if the check passes does main() start running. That order is the most important habit to take from this lesson: when something is wrong, the message you see comes from the checking stage, and nothing has run yet.

1. Write

Save your code in a file that ends in .dart. Every runnable file needs a main() function, because that is where Dart starts.

2. Check

dart run reads the whole file first. Typos, missing semicolons and values of the wrong type are reported with a file name, a line and a column.

3. Run

If the check passes, main() executes from its first statement to its last, and print() writes text to the terminal.

This course teaches Dart 3

In Dart 3 sound null safety is always on, so there is no older null-unsafe mode to switch to. Records, patterns and class modifiers are part of the language too. These pages do not quote an exact release number; check the official release notes for that.

Mental model: write, check, run
  1. Write a .dart file
  2. Dart checks names, types and ;
  3. main() runs, print shows output

If the check fails nothing runs, so the first error message is your best teacher.

General pattern: a Dart file

dart
ReturnType functionName(Type parameter) {
  return value;
}

void main() {
  // every statement ends with a semicolon;
  functionName(argument);
}

A Dart file is a list of function declarations. Each function names its return type first, then its name and typed parameters. Execution starts at main(), which is why every runnable program has one. void means main() gives nothing back.

Your first Dart program

Read this program before you run it. It declares one function that builds a greeting, and main() calls it and then prints a progress line. Predict the two lines it will print, then run it with dart run welcome.dart (the next lesson, Get Started, shows how) or paste it into DartPad.

welcome.dart
// welcome.dart: your first Dart program
String welcome(String name, int lessonsLeft) {
  return 'Hi $name, $lessonsLeft lessons to go!';
}

void main() {
  const total = 29;
  var done = 1;
  print(welcome('Aye Aye', total - done));
  print('Course progress: ${done * 100 ~/ total}%');
}

Code breakdown

  1. A typed functionString welcome(String name, int lessonsLeft) {

    One line makes three promises: name is a String, lessonsLeft is an int, and the function returns a String. A call with the arguments swapped, such as welcome(1, 'Aye'), is rejected.

  2. String interpolationreturn 'Hi $name, $lessonsLeft lessons to go!';

    Inside the string, $name and $lessonsLeft are replaced by the variables' values. You never have to glue a number onto text by hand.

  3. A constantconst total = 29;

    const marks a value that is known while compiling and never changes. No type is written, yet Dart knows it is an int from the 29.

  4. A variablevar done = 1;

    Declared with var, done could be changed later, but its type stays int. Lesson 5 compares var, final and const in detail.

  5. An expression as an argumentprint(welcome('Aye Aye', total - done));

    total - done is worked out first and gives 28, an int that fits the lessonsLeft parameter. print() then writes the String that welcome() returned.

  6. Integer divisionprint('Course progress: ${done * 100 ~/ total}%');

    Any expression fits inside ${ }. The ~/ operator is integer division, so 100 ~/ 29 is 3. The ordinary / would give a double such as 3.44...

You should see
Hi Aye Aye, 28 lessons to go!
Course progress: 3%
From welcome.dart to output
  1. Write welcome.dart
  2. dart run reads and checks the file
  3. main() starts running
  4. print writes each line to the terminal

The check passes

Both lines are printed, then the program ends

The check fails

No program runs; the errors list file, line and column

How this course is organized

The course has 29 lessons in five chapters, and each chapter builds on the one before it. Go through them in order: the async lessons assume you already know functions, collections and null safety.

ChapterLessonsWhat you learn
Basic10What Dart is, setup, syntax, variables, data types, functions, operators, Lists and Maps
Intermediate7If/else, loops, null safety, classes, constructors, inheritance and mixins
Advanced8Records and patterns, generics, sealed types, Futures and Streams, isolates, packages, testing
Projects2A mini project and a summary with next steps
Exercises2Practice sets for fundamentals and for OOP and async code

How to work through a lesson

Every lesson follows the same order: a plain explanation, a problem, the core concept, the general pattern, a worked example explained line by line, common mistakes, a mini challenge and three quiz questions. A routine that works well looks like this.

1. Predict

Before running an example, write down the output you expect.

2. Run

Run it in DartPad or with dart run and compare the result with your guess.

3. Break it on purpose

Remove a semicolon or change one type, then read the error message you get.

4. Do the challenge

Fill in the TODOs in the starter code yourself before you open the solution.

In real projects

Dart in real projects

Most people meet Dart through Flutter: one Dart codebase can produce apps for Android, iOS, the web and desktop, and every widget, tap handler and network call in those apps is Dart code. Because a Flutter app is plain Dart plus a framework, the skills in this course transfer directly. A Flutter developer who cannot read a null-safety error or an async function is slowed down in every file.

Dart is also used outside Flutter. Command-line tools and small servers are written in Dart, and the dart compile command can turn a program into a self-contained executable. Libraries are shared through the pub.dev package site, and teams keep code tidy by running dart format and dart analyze automatically before every merge.

Common mistakes

Common beginner mistakes

1. Typing JavaScript or Python from memory

Habits like function, let, console.log or a missing semicolon produce a long list of errors. Read only the first error, fix it and run again; the later errors are often just consequences of the first.

2. Starting Flutter before plain Dart

A Flutter error message mixes Dart language errors with framework errors, and it is hard to tell which is which. Run plain Dart files first, so a type error or a null error is already familiar when it appears inside a widget.

3. Running examples without predicting the output

Copying and running an example only proves that you can copy. Before each run, write down the lines you expect, then compare. A difference between your guess and the real output is exactly where a misunderstanding is hiding.

Mini challenge

Mini challenge: a progress line

Write a typed function progressLine that takes the number of finished lessons and the total, and returns a string such as 6/29 lessons done (20%). Then call it from main() and print three results.

  • Give both parameters the type int and the function the return type String
  • Work out the percentage with integer division ~/
  • When total is 0, show 0% instead of dividing by zero
  • In main(), print the results for (6, 29), (29, 29) and (0, 0), and predict the output before running
progress.dart
// TODO: give both parameters the type int and the function a String return type
progressLine(done, total) {
  // TODO: if total is 0 the percentage is 0 (never divide by zero)
  // TODO: otherwise use integer division on done * 100
  // TODO: return text such as '6/29 lessons done (20%)'
}

void main() {
  // TODO: print the progress line for 6 of 29, 29 of 29 and 0 of 0
}

Expected: The program prints these three lines: 6/29 lessons done (20%) 29/29 lessons done (100%) 0/0 lessons done (0%)

Show hint

Work out percent first: total == 0 ? 0 : done * 100 ~/ total. Then build the text with interpolation: '$done/$total lessons done ($percent%)'. Multiplying done by 100 before the division keeps the result from collapsing to 0.

Show solution
progress.dart
String progressLine(int done, int total) {
  final percent = total == 0 ? 0 : done * 100 ~/ total;
  return '$done/$total lessons done ($percent%)';
}

void main() {
  print(progressLine(6, 29));
  print(progressLine(29, 29));
  print(progressLine(0, 0));
}

Quick quiz 1 / 3

Which statement about Dart is correct?

Quick quiz 2 / 3

With done equal to 1 and total equal to 29, what does print('Course progress: ${done * 100 ~/ total}%'); print?

Quick quiz 3 / 3

You type function main() { let name = "Aye"; } into hello.dart and run it. What happens?

What to remember

  • Dart is a typed, null-safe language from Google used for Flutter apps, command-line tools and servers
  • Dart works as write, check, run, and if the check fails nothing runs
  • Every runnable file needs main(), and each statement ends with a semicolon
  • Work through the 29 lessons in order: Basic, Intermediate, Advanced, Projects, Exercises

Dart 3.13 changelog — Dart

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Dart HOME | Thuta Learning