Dart Programming Fundamentals

Introduction to Dart & Setting Up Your Environment

25 min Lesson 1 of 6

What is Dart?

Dart is a modern, open-source programming language developed by Google. It was first released in 2011 and has since become the primary language for building applications with Flutter. Dart is designed to be easy to learn, fast to execute, and capable of running on multiple platforms -- mobile, web, desktop, and server.

Dart is a statically typed, object-oriented language with a syntax that feels familiar if you know JavaScript, Java, or C#. It compiles to native machine code for mobile and desktop, to JavaScript for the web, and runs on its own virtual machine (Dart VM) during development.

Why Learn Dart?

Here are the key reasons Dart is worth learning:

  • Flutter’s Language -- Dart is the only language used by Flutter, the most popular cross-platform framework for building mobile, web, and desktop apps from a single codebase.
  • Easy to Learn -- If you know any C-style language (JavaScript, Java, C#), Dart’s syntax will feel natural and intuitive.
  • Fast Performance -- Dart compiles to native ARM and x64 machine code, giving near-native performance on mobile devices.
  • Hot Reload -- During development, Dart’s JIT (Just-In-Time) compilation enables Flutter’s famous hot reload, letting you see changes instantly.
  • Null Safety -- Dart has built-in sound null safety, which eliminates an entire class of runtime errors at compile time.
  • Growing Ecosystem -- With thousands of packages on pub.dev, you can extend your apps with ready-made solutions.

Dart’s Compilation Modes

Dart uses two different compilation strategies depending on the context:

  • JIT (Just-In-Time) -- Used during development. Code is compiled on the fly, enabling features like hot reload and debugging. Slightly slower execution but faster development cycle.
  • AOT (Ahead-Of-Time) -- Used for production builds. Code is compiled to native machine code before execution, resulting in fast startup times and optimized performance.
Note: This dual compilation approach is one of Dart’s biggest advantages. You get the best of both worlds: fast development with JIT and fast production apps with AOT.

Setting Up Dart

There are several ways to set up Dart on your machine. Since we will eventually use Flutter, we will cover both standalone Dart and the Flutter SDK (which includes Dart).

Option 1: Install Dart Standalone

If you want to learn Dart before diving into Flutter:

macOS (using Homebrew)

brew tap dart-lang/dart
brew install dart

Windows (using Chocolatey)

choco install dart-sdk

Linux (using apt)

sudo apt update
sudo apt install dart

Option 2: Install Flutter SDK (Includes Dart)

When you install Flutter, the Dart SDK is included automatically. This is the recommended approach since you will need Flutter later in this course.

Verify Installation

dart --version
# Output: Dart SDK version: 3.x.x

Option 3: Use DartPad (No Installation Needed)

If you want to start coding immediately without installing anything, visit DartPad at dartpad.dev. It is an online editor where you can write, run, and share Dart code directly in your browser.

Pro Tip: DartPad is excellent for quick experiments and following along with lessons. However, for real projects, you will need a local installation with an IDE like VS Code or Android Studio.

Your First Dart Program

Every Dart program starts with a main() function. This is the entry point -- the first function Dart executes when you run your program.

Hello World in Dart

void main() {
  print('Hello, World!');
}

Let us break this down:

  • void -- The return type. void means this function does not return any value.
  • main() -- The function name. Dart always looks for main() as the starting point.
  • print() -- A built-in function that outputs text to the console.
  • 'Hello, World!' -- A string literal enclosed in single quotes.

Running Your Program

# Save the code in a file called hello.dart
# Then run it from the terminal:
dart run hello.dart

# Output:
# Hello, World!

Dart Program Structure

A typical Dart file has the following structure:

Basic Dart File Structure

// 1. Import statements (libraries you need)
import 'dart:math';

// 2. Top-level variables (optional)
const String appName = 'My First App';

// 3. Main function (entry point)
void main() {
  print('Welcome to $appName');
  print('Random number: ${Random().nextInt(100)}');
}

// 4. Other functions and classes (optional)
String greet(String name) {
  return 'Hello, $name!';
}

Key observations:

  • Every statement ends with a semicolon (;).
  • Single-line comments use // and multi-line comments use /* */.
  • Dart uses string interpolation with $variable or ${expression} inside strings.
  • Functions are defined with a return type, name, and parameters.

Setting Up Your IDE

For the best development experience, use VS Code or Android Studio with Dart and Flutter extensions.

VS Code Setup

  1. Install VS Code from code.visualstudio.com.
  2. Open Extensions (Ctrl+Shift+X or Cmd+Shift+X).
  3. Search for and install the Dart extension by Dart Code.
  4. Optionally install the Flutter extension (which also installs Dart).

The Dart extension provides syntax highlighting, code completion, debugging, refactoring tools, and integrated Dart analysis.

Dart vs Other Languages

If you are coming from another language, here is how Dart compares:

  • vs JavaScript -- Dart is statically typed (types are checked at compile time), has sound null safety, and compiles to native code. JavaScript is dynamically typed and runs in browsers.
  • vs Java -- Dart has a more concise syntax, no need for new keyword, built-in async/await, and better type inference.
  • vs Swift/Kotlin -- Similar modern features, but Dart targets all platforms through Flutter, not just iOS or Android.

Dart vs JavaScript Comparison

// JavaScript
function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet("World"));

// Dart
String greet(String name) {
  return 'Hello, $name!';
}
void main() {
  print(greet('World'));
}
Common Mistake: Unlike JavaScript, Dart requires you to declare the type of a function’s parameters and return value (or use var/dynamic for flexibility). Forgetting types will cause compile-time errors. This strictness is actually a benefit -- it catches bugs before your code runs.

Practice Exercise

Open DartPad at dartpad.dev and write a Dart program that prints your name, your favorite programming language, and why you want to learn Dart. Use string interpolation with the $ syntax. For example: print('My name is $name');. Experiment with creating a separate function that returns a greeting string.