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.
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.
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 ?.
- Numbers
- Dart int
double
Stringbool- Dart List
- Dart Set
- Dart Map
- Records
- Runes
- Symbols
Nulland nullable types
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.
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 Operators
- Dart Logical Operators
- Equality and relational operators
- Assignment and compound assignment operators
- Type-test operators
- Null-aware operators
Dart Arithmetic Operator Tutorials
- Dart Addition — Add numeric operands with the
+operator. - Dart Subtraction — Subtract one number from another.
- Dart Multiplication — Calculate the product of two numbers.
- Dart Unary Minus — Reverse the sign of a numeric value.
- Dart Division — Calculate a division result with
/. - Dart integer division — Calculate an integer quotient with
~/. - Dart Modulo Division — Find the remainder with
%. - Dart Increment — Increase a numeric variable by one.
- Dart Decrement — Decrease a numeric variable by one.
Dart Logical Operator Tutorials
- Dart Logical AND — Return true when both conditions are true.
- Dart Logical OR — Return true when at least one condition is true.
- Dart Logical NOT — Reverse a Boolean value.
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.
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 Recursion Function — Define a function that calls itself with a smaller subproblem.
Dart Exception Handling
- Dart Try-Catch — Catch exceptions, inspect errors, and run cleanup code with
finally.
Dart Console Input and Output
- Read an Integer from the Console in Dart
- Read a String from the Console in Dart
- Print a string to the Dart console with
print(). - Print Without a Newline in Dart
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.
- Check Whether a Dart String Contains Another String
- Check Whether a Dart String Starts with Specific Text
- Check Whether a Dart String Ends with Specific Text
- Check Whether a Dart String Is Empty
- Check Whether Two Dart Strings Are Equal
- Compare Strings in Dart
- Concatenate Strings in Dart
- Concatenate a Dart String with Itself Multiple Times
- Convert a Dart String to Uppercase
- Convert a Dart String to Lowercase
- Convert a Dart String into a List of Characters
- Find the Index of a Substring in a Dart String
- Get the First Character of a Dart String
- Get the Last Character of a Dart String
- Get a Character at a Specific Dart String Index
- Iterate over the Characters of a Dart String
- Replace a Substring in a Dart String
- Split a Dart String
- Split a Dart String by a Comma
- Split a Dart String by a Space
- Get a Substring from a Dart String
- Find the Length of a Dart String
- Trim Whitespace from a Dart String
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.
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 Lists
- Find the Length of a Dart List
- Check Whether a Dart List Is Empty
- Check Whether Two Dart Lists Are Equal
- Iterate over a Dart List
- Add an Element to a Dart List
- Check Whether a Dart List Contains an Element
- Check Whether Any Dart List Element Satisfies a Test
- Check Whether Every Dart List Element Satisfies a Test
- Get a Dart List Element at a Specific Index
- Join Two Dart Lists
- Use forEach() with a Dart List
- Reverse a Dart List
- Remove All Elements from a Dart List
- Shuffle a Dart List
- Convert a Dart List to a Set
Dart Set Tutorials
A Dart Set stores unique elements. Adding the same value more than once does not create duplicate entries.
- Dart Sets
- Find the Length of a Dart Set
- Check Whether a Dart Set Is Empty
- Add an Element to a Dart Set
- Check Whether a Dart Set Contains an Element
- Filter Elements of a Dart Set
- Remove an Element from a Dart Set
- Remove Dart Set Elements Based on a Condition
- Find the union of Dart Sets
- Find the Intersection of Dart Sets
- Iterate over a Dart Set Using for-in
- Iterate over a Dart Set Using forEach()
- Reduce Dart Set Elements to One Value
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.
(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.
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
- Get the Current Date and Time in Dart
- Create, parse, compare, and format Dart
DateTimevalues. - Represent a span of time with Dart
Duration.
Dart Practice Programs
Use these programs after covering variables, operators, conditions, and loops. Try writing each solution independently before comparing the implementation.
- Dart Program to Find the Sum of Two Numbers
- Dart Program to Find the Square Root of a Number
- Dart Program to Find the Average of Two Numbers
- Dart Program to Find the Sum of the First N Natural Numbers
- Dart Program to Find the Sum of Squares of the First N Natural Numbers
- Dart Program to Check Whether a Number Is Prime
- Dart Program to Print Prime Numbers in a Range
- Dart Program to Find the Factorial of a Number
- Dart Program to Find the Factors of a Number
- Dart Program to Find the Product of the Digits in a Number
- Dart Program to Find the Sum of the Digits in a Number
Recommended Dart Learning Order
- Install Dart or open DartPad, and run a Hello World program.
- Learn variables, built-in data types, type inference, and null safety.
- Practise arithmetic, comparison, logical, and null-aware operators.
- Write programs with if statements, loops, break, and continue.
- Define functions and work with positional and named parameters.
- Learn String, List, Set, and Map operations.
- Study classes, inheritance, interfaces, mixins, enums, and generics.
- Continue with exceptions, futures, streams, records, and patterns.
- 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.
TutorialKart.com