Introduction

Dart is the programming language behind Flutter, Google’s powerful cross-platform UI toolkit. If you are new to Dart, the best way to start learning is by writing a simple program, and nothing is more classic than a “Hello World” program.

In this guide, we will explore:

  • How to write your first Dart program.
  • Understanding Dart syntax and structure.
  • Running Dart code in different environments.
  • Modifying the Hello World program to make it interactive.
  • Best practices for beginners.

By the end, you will have a strong foundation to start building Dart programs confidently.


What is Dart?

Dart is a modern, object-oriented, strongly-typed programming language designed for fast development and high-performance applications. Key features include:

  • Object-oriented syntax similar to Java or C#.
  • Sound null safety for preventing runtime null errors.
  • Asynchronous programming support with async and await.
  • Cross-platform support for mobile, web, desktop, and server.

Setting Up Dart Environment

Before writing your first program, you need to set up Dart.

Option 1: Using DartPad (Online)

  • Visit https://dartpad.dev.
  • This online IDE allows you to write and run Dart code without installing anything.
  • Ideal for beginners to experiment.

Option 2: Installing Dart SDK Locally

  1. Go to https://dart.dev/get-dart.
  2. Download the SDK for your operating system (Windows/macOS/Linux).
  3. Extract the SDK and add it to your system PATH.
  4. Verify installation by running:
dart --version

Option 3: Using Visual Studio Code

  • Install VS Code.
  • Install Dart extension from the marketplace.
  • Create a new Dart file (main.dart) and start coding.

Writing Your First Dart Program

Step 1: Create a Dart File

Create a file named main.dart. This is the conventional entry point for Dart programs.

Step 2: Write Code

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

Step 3: Understanding the Code

  • void main() – The main function where execution starts. Every Dart program requires a main function.
  • print('Hello, World!'); – Prints the string Hello, World! to the console.
  • ; – Marks the end of a statement.

Running Your Dart Program

Using DartPad

  • Paste the code into DartPad.
  • Click Run.
  • Output:
Hello, World!

Using Command Line

  1. Open terminal and navigate to the directory containing main.dart.
  2. Run:
dart run main.dart
  1. The console will display:
Hello, World!

Using VS Code

  1. Open the file in VS Code.
  2. Press F5 or click Run → Start Debugging.
  3. The console displays:
Hello, World!

Making It Interactive

You can extend the Hello World program to accept input from users.

import 'dart:io';

void main() {
  stdout.write('Enter your name: ');
  String? name = stdin.readLineSync();
  print('Hello, $name!');
}

How It Works

  • import 'dart:io'; – Imports the Input/Output library for console interaction.
  • stdout.write() – Prints text without a newline.
  • stdin.readLineSync() – Reads user input from the console.
  • $name – String interpolation to include user input in the output.

Adding Comments

Comments help explain your code. Dart supports:

// Single-line comment
/* Multi-line
   comment */

Example:

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

Exploring Basic Dart Concepts

Even in a simple Hello World program, you can learn:

  1. Variables
String greeting = 'Hello, World!';
print(greeting);
  1. Functions
void sayHello(String name) {
  print('Hello, $name!');
}
sayHello('Ali');
  1. Data Types
  • String, int, double, bool
int age = 25;
double price = 9.99;
bool isActive = true;
  1. Conditionals
if (age > 18) {
  print('Adult');
} else {
  print('Minor');
}

Best Practices for Beginners

  • Always use meaningful variable names.
  • Add comments to explain logic.
  • Start experimenting with small modifications to Hello World.
  • Learn to run Dart programs in multiple environments (DartPad, terminal, VS Code).
  • Gradually explore functions, loops, and classes.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *