Dart Tutorial for Beginners

This Dart tutorial provides a structured path for learning the Dart programming language, from variables and data types to collections, functions, exception handling, object-oriented programming, and practice programs.

Dart is a strongly typed programming language with type inference, sound null safety, asynchronous programming support, pattern matching, records, and object-oriented features. It is the language used by Flutter, and it can also be used for command-line, server, and web applications.

What You Will Learn in This Dart Tutorial

  • How to install and run Dart programs.
  • Variables, built-in types, operators, and null safety.
  • Conditional statements and loops.
  • Functions, console input, output, and exception handling.
  • String, List, Set, and Map operations.
  • Classes, inheritance, method overriding, enums, and object types.
  • Small Dart programs for practising programming logic.

Prerequisites for Learning Dart

You do not need previous programming experience to begin this tutorial. The lessons introduce the required concepts through syntax and focused examples.

Experience with Java, JavaScript, C#, Kotlin, or another programming language can make some topics familiar, but it is not required.

Dart Editors and Development Tools

DartPad is suitable for trying short Dart examples in a web browser without installing the SDK. For local development, install the Dart SDK and use an editor with Dart support.

Visual Studio Code with the Dart extension and IntelliJ-based editors are commonly used for Dart development. The Flutter SDK already includes the Dart SDK, so a separate Dart installation is usually unnecessary when working only on Flutter projects.

Run a First Dart Program

Every standalone Dart program starts execution from a top-level main() function. The following program declares a variable and prints its value.

</>
Copy
void main() {
  String language = 'Dart';
  print('Learning $language');
}

Output

Learning Dart

Save a local program in a file such as main.dart, and run it from a terminal with the following command.

</>
Copy
dart run main.dart

Dart Programming Basics

Begin with installation, program structure, variables, comments, and Dart’s built-in types before moving to decisions, loops, and functions.

Set Up Dart and Write Your First Programs

  • Install Dart on Windows — Install the Dart SDK and verify the installation on a Windows computer.
  • Dart Hello World Program — Understand the main() function and print text to standard output.
  • Dart Variables — Declare variables, use type inference, assign values, and update variable values.
  • Dart Comments — Write single-line, multiline, and documentation comments.

Dart Built-in Data Types

Dart has built-in support for numbers, strings, Boolean values, records, functions, symbols, runes, and collection types. Variables are non-nullable by default under sound null safety unless their type includes ?.

Dart Null Safety Example

A variable such as String name cannot contain null. Add ? to the type when a null value is valid, and account for that possibility before using the value.

</>
Copy
void main() {
  String? nickname;

  print(nickname?.toUpperCase() ?? 'No nickname');

  nickname = 'Sam';
  print(nickname.toUpperCase());
}

Output

No nickname
SAM

Dart Conditional Statements

  • Dart If — Execute a block only when a Boolean condition is true.
  • Dart If-Else — Choose between two blocks based on a condition.
  • Dart If-Else-If — Evaluate several conditions in sequence.

Dart Loop Statements and Loop Control

  • Dart While Loop — Repeat a block while its condition remains true.
  • Dart Do-While Loop — Run a block once before checking its continuation condition.
  • Dart For Loop — Repeat code with initialization, condition, and update expressions.
  • Dart Break — Exit a loop before its normal completion.
  • Dart Continue — Skip the remaining statements in the current iteration.

Dart Operators

Operators perform arithmetic, comparisons, logical tests, assignments, type checks, null-aware operations, and other calculations.

Dart Arithmetic Operator Tutorials

Dart Logical Operator Tutorials

Dart Functions, Exceptions, and Console Input

Dart Function Fundamentals

A Dart function can accept positional or named parameters, return a typed value, and use arrow syntax for a single expression. Functions can also be asynchronous and return a Future.

</>
Copy
int add(int a, int b) {
  return a + b;
}

String greet({required String name}) => 'Hello, $name';

void main() {
  print(add(10, 5));
  print(greet(name: 'Maya'));
}

Output

15
Hello, Maya

Dart Exception Handling

  • Dart Try-Catch — Catch exceptions, inspect errors, and run cleanup code with finally.

Dart Console Input and Output

Dart String Operations

Dart strings are immutable sequences of UTF-16 code units. The following tutorials cover searching, comparison, concatenation, case conversion, indexing, replacement, splitting, substrings, length, trimming, and character iteration.

Dart Collections: List, Set, and Map

Dart provides collection types for storing and processing groups of values. A List is ordered and index-based, a Set stores unique values, and a Map associates keys with values. Generic type arguments can restrict the values accepted by a collection.

</>
Copy
void main() {
  List<String> languages = ['Dart', 'Kotlin'];
  Set<String> uniqueLanguages = {'Dart', 'Dart', 'Swift'};
  Map<String, int> scores = {'Asha': 88, 'Ravi': 91};

  print(languages);
  print(uniqueLanguages);
  print(scores['Ravi']);
}

Output

[Dart, Kotlin]
{Dart, Swift}
91

Dart List Tutorials

Dart Set Tutorials

A Dart Set stores unique elements. Adding the same value more than once does not create duplicate entries.

Dart Map Topics

A Dart Map stores key-value pairs. Keys identify entries and should be unique within the Map.

  • Dart Maps
  • Find the length of a Dart Map
  • Check whether a Dart Map is empty
  • Add entries to a Dart Map
  • Check whether a Dart Map contains a key
  • Check whether a Dart Map contains a value
  • Remove an entry from a Dart Map
  • Iterate over the entries of a Dart Map

Dart Runes, Symbols, Records, and Patterns

Runes provide access to Unicode code points in a string, while Symbols represent identifiers in contexts such as reflection. Modern Dart also supports records for grouping multiple typed values and patterns for matching or destructuring data.

</>
Copy
(String, int) getStudent() {
  return ('Asha', 92);
}

void main() {
  final (name, score) = getStudent();
  print('$name scored $score');
}

Output

Asha scored 92

Dart Object-Oriented Programming

Dart is object-oriented: values are objects, and classes define their state and behaviour. Dart supports constructors, inheritance, abstract classes, interfaces, mixins, extension methods, enums, generics, and class modifiers.

  • Dart Class — Define fields, constructors, methods, and objects.
  • Dart Inheritance — Extend a base class and reuse inherited members.
  • Dart Method Overriding — Replace inherited method behaviour in a subclass.
  • Dart Enum — Represent a fixed set of named values.
  • Dart interfaces and implicit interface implementation
  • Dart abstract classes and mixins
  • Dart generics and class modifiers

Inspect a Dart Object’s Runtime Type

Dart Asynchronous Programming

Dart represents a result that may become available later with Future. Use async and await to write asynchronous code in a sequential form. A Stream represents a sequence of asynchronous events.

</>
Copy
Future<String> loadMessage() async {
  await Future.delayed(const Duration(milliseconds: 100));
  return 'Data loaded';
}

Future<void> main() async {
  final message = await loadMessage();
  print(message);
}

Output

Data loaded

Dart Date, Time, and Duration

Dart Practice Programs

Use these programs after covering variables, operators, conditions, and loops. Try writing each solution independently before comparing the implementation.

Recommended Dart Learning Order

  1. Install Dart or open DartPad, and run a Hello World program.
  2. Learn variables, built-in data types, type inference, and null safety.
  3. Practise arithmetic, comparison, logical, and null-aware operators.
  4. Write programs with if statements, loops, break, and continue.
  5. Define functions and work with positional and named parameters.
  6. Learn String, List, Set, and Map operations.
  7. Study classes, inheritance, interfaces, mixins, enums, and generics.
  8. Continue with exceptions, futures, streams, records, and patterns.
  9. Build small command-line programs before moving to larger Dart or Flutter projects.

Dart Tutorial Questions

Is Dart suitable for a programming beginner?

Yes. Dart has readable syntax, static analysis, type inference, and tools that identify many mistakes while code is being written. A beginner can start in DartPad without configuring a local project.

Do I need Flutter to learn Dart?

No. Dart is a programming language that can be learned and used independently. Flutter uses Dart, but Dart can also be used for command-line, server, and web programs.

Does the Flutter SDK include Dart?

Yes. The Flutter SDK includes the Dart SDK and the tools required to compile and run Dart code used in Flutter projects.

What is sound null safety in Dart?

Sound null safety makes types non-nullable by default. A variable can contain null only when its type explicitly permits it, such as String?. Static analysis then requires the nullable case to be handled before unsafe member access.

What should I learn after Dart basics?

After variables, operators, conditions, loops, and functions, continue with collections, classes, generics, exception handling, asynchronous programming, records, patterns, packages, testing, and the tools used by the type of Dart application you plan to build.

Dart Tutorial Summary

This Dart Tutorial organizes the language into a practical learning sequence covering setup, syntax, null safety, control flow, operators, functions, strings, collections, object-oriented programming, asynchronous code, date and time values, and practice programs.