Introduction to dart basics

Module Introduction

Dart Basics

This section introduces the core building blocks of the Dart programming language. Every Flutter developer must be comfortable with these concepts before moving to widgets and UI.

All examples here are DartPad-ready, meaning you can copy and run them directly in DartPad.


1. What is DartPad?

DartPad is an online editor that lets you write and execute Dart code directly in the browser. It removes setup complexity and allows you to focus purely on understanding the language.

It is ideal for:

  • Learning Dart syntax

  • Testing logic quickly

  • Understanding language behavior

void main() {
  print('Welcome to DartPad');
}

2. Variables in Dart

Variables are used to store data. Dart is a type-safe language, meaning each variable has a data type.

Declaring Variables

void main() {
  int age = 22;
  String name = 'Rabin';
  double height = 5.9;
  bool isStudent = true;

  print(age);
  print(name);
  print(height);
  print(isStudent);
}

Using var and final

void main() {
  var city = 'Kathmandu'; // type inferred
  final country = 'Nepal'; // cannot be reassigned

  city = 'Pokhara';
  // country = 'India'; // error

  print(city);
  print(country);
}

3. Keywords in Dart

Keywords are reserved words with special meaning in Dart and cannot be used as variable names.

Common Dart keywords:

  • var, final, const

  • if, else, switch

  • for, while, do

  • class, extends

  • return, void

Example usage:

void main() {
  final int score = 80;

  if (score >= 40) {
    print('Passed');
  } else {
    print('Failed');
  }
}

4. Data Types in Dart

Dart supports several built-in data types.

Common Data Types

void main() {
  int count = 10;
  double price = 99.99;
  String message = 'Hello Dart';
  bool isActive = false;

  print(count);
  print(price);
  print(message);
  print(isActive);
}

dynamic Type

void main() {
  dynamic value = 'Text';
  print(value);

  value = 100;
  print(value);
}

Use dynamic carefully. In Flutter projects, it is generally avoided unless necessary.


5. Functions in Dart

Functions are reusable blocks of code that perform a specific task.

Basic Function

void greet() {
  print('Hello from Dart');
}

void main() {
  greet();
}

Function with Parameters

void greetUser(String name) {
  print('Hello, $name');
}

void main() {
  greetUser('Rabin');
}

Function with Return Value

int add(int a, int b) {
  return a + b;
}

void main() {
  print(add(5, 3));
}

6. Collection Data Types

Collections are used to store multiple values in a single variable.

List

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Mango'];

  fruits.add('Orange');

  print(fruits);
  print(fruits[0]);
}

Set

void main() {
  Set<int> numbers = {1, 2, 3, 3};

  print(numbers); // duplicates removed
}

Map

void main() {
  Map<String, dynamic> student = {
    'name': 'Rabin',
    'age': 22,
    'isActive': true
  };

  print(student['name']);
  print(student['age']);
}

Summary

In this Dart Basics section, you learned:

  • What DartPad is and why it is used

  • How to declare variables

  • Common Dart keywords

  • Core data types

  • Writing and using functions

  • Working with lists, sets, and maps

These concepts form the foundation of Flutter development. In the next section, we will use this knowledge to start building Flutter widgets and layouts.


Next: Flutter Project Structure & Widgets

Updated on