TechLearn Hub

Learn Technology in Simple English

What Is Dart?

Dart is a modern programming language developed by Google. It is designed for building fast applications across different platforms.

Dart is especially well known because it is the programming language used by Flutter for creating applications.

With Dart and Flutter, developers can create applications for Android, iOS, web, desktop, and other supported platforms.

Who Created Dart?

Dart was developed by Google and was introduced in 2011.

The language has continued to evolve over time and has become closely connected with Google's Flutter framework.

Why Is Dart Important?

Dart is important because it provides:

Where Is Dart Used?

Dart can be used for:

However, Dart is most commonly associated with Flutter application development.

How Does Dart Work?

Dart programs can be compiled and executed in different ways depending on the target platform and development environment.

For Flutter applications, Dart can be compiled into efficient native code for supported platforms.

Dart allows developers to use one programming language with Flutter to target multiple platforms.

Your First Dart Program

A simple Dart program looks like this:

void main() {
  print("Hello, world!");
}

The main() function is the starting point of a basic Dart program.

Variables in Dart

Variables store information that a program can use.

void main() {
  var name = "Ahmed";
  var age = 20;

  print(name);
  print(age);
}

Using var

The var keyword allows Dart to determine a variable's type from its initial value.

var name = "Ahmed";
var age = 20;

Final and Const

Dart provides final and const for values that should not be reassigned.

final name = "Ahmed";

const maxScore = 100;

A final variable is assigned once, while a const value must be a compile-time constant.

Dart Data Types

TypeExample
String"Hello"
int25
double25.5
booltrue
List[1, 2, 3]
Map{"name": "Ahmed"}

Strings

Strings are used to store text.

String firstName = "Ahmed";
String lastName = "Ali";

String fullName = "$firstName $lastName";

void main() {
  print(fullName);
}

Operators

Dart supports mathematical, comparison, and logical operators.

void main() {
  int a = 10;
  int b = 5;

  print(a + b);
  print(a - b);
  print(a * b);
  print(a / b);
}

If and Else

Conditional statements allow programs to make decisions.

void main() {
  int age = 20;

  if (age >= 18) {
    print("Adult");
  } else {
    print("Minor");
  }
}

Else If

Multiple conditions can be checked using else if.

void main() {
  int score = 75;

  if (score >= 80) {
    print("Excellent");
  } else if (score >= 60) {
    print("Good");
  } else {
    print("Keep practicing");
  }
}

Switch Statements

A switch statement can be used when a value has several possible cases.

void main() {
  String day = "Monday";

  switch (day) {
    case "Monday":
      print("Start of the week");
      break;
    case "Friday":
      print("End of the work week");
      break;
    default:
      print("Another day");
  }
}

For Loops

A for loop repeats code for a sequence of values.

void main() {
  for (int i = 1; i <= 5; i++) {
    print(i);
  }
}

While Loops

void main() {
  int number = 1;

  while (number <= 5) {
    print(number);
    number++;
  }
}

Functions

Functions are reusable blocks of code.

void greet() {
  print("Hello!");
}

void main() {
  greet();
}

Function Parameters

Functions can receive values through parameters.

void greet(String name) {
  print("Hello, $name!");
}

void main() {
  greet("Ahmed");
}

Return Values

A function can calculate something and return a result.

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

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

Arrow Functions

Short functions can sometimes be written using arrow syntax.

int square(int number) => number * number;

void main() {
  print(square(5));
}

Lists

A List stores multiple values in an ordered collection.

void main() {
  List<int> numbers = [10, 20, 30, 40];
  print(numbers[0]);
}

Adding Items to a List

void main() {
  List<String> names = ["Ahmed", "Ali"];
  names.add("Umar");
  print(names);
}

Maps

A Map stores information using key-value pairs.

void main() {
  Map<String, dynamic> user = {
    "name": "Ahmed",
    "age": 20
  };

  print(user["name"]);
}

Sets

A Set stores unique values.

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

Classes

Classes allow developers to create custom objects.

class User {
  String name;
  int age;

  User(this.name, this.age);
}

void main() {
  User user = User("Ahmed", 20);
  print(user.name);
}

Objects

An object is an instance of a class.

void main() {
  User user = User("Ahmed", 20);
  print(user.name);
}

The object contains the data and behavior defined by its class. The example uses the User class defined in the previous section.

Constructors

Constructors are used when creating objects.

class Car {
  String brand;

  Car(this.brand);
}

void main() {
  Car car = Car("Toyota");
  print(car.brand);
}

Inheritance

Dart supports inheritance, which allows one class to reuse functionality from another class.

class Animal {
  void speak() {
    print("Animal sound");
  }
}

class Dog extends Animal {
  void bark() {
    print("Woof");
  }
}

void main() {
  Dog dog = Dog();
  dog.speak();
  dog.bark();
}

Abstract Classes

Abstract classes can define a common structure for other classes.

abstract class Animal {
  void speak();
}

class Dog extends Animal {
  @override
  void speak() {
    print("Woof");
  }
}

void main() {
  Dog dog = Dog();
  dog.speak();
}

What Is Null Safety?

Null safety helps developers identify variables that can contain null and variables that are expected to always contain a value.

String name = "Ahmed";

String? nickname;

The question mark means that nickname can contain null.

Understanding null safety is important when developing modern Dart and Flutter applications.

Nullable Values

void main() {
  String? name;

  name = "Ahmed";

  print(name);
}

Before using nullable values where a non-null value is required, safely handle the possibility of null.

Null-Aware Operators

Dart provides operators that make working with nullable values easier.

void main() {
  String? name;
  print(name ?? "Unknown");
}

The ?? operator provides a fallback value when the value on the left is null.

Enums

Enums represent a fixed set of related values.

enum Direction {
  north,
  south,
  east,
  west
}

void main() {
  Direction direction = Direction.north;
  print(direction);
}

What Is Async Programming?

Asynchronous programming allows an application to perform operations that may take time without unnecessarily blocking other work.

This is useful when working with networks, files, databases, and other operations that may take time.

Future

A Future represents a value that may become available later.

Future<String> getMessage() async {
  return "Hello from the future";
}

void main() async {
  print(await getMessage());
}

Async and Await

The async and await keywords make asynchronous code easier to read.

Future<String> getMessage() async {
  return "Data loaded";
}

Future<void> loadData() async {
  String message = await getMessage();
  print(message);
}

Future<void> main() async {
  await loadData();
}

Streams

A Stream can provide multiple values over time.

Streams are useful for events, live data, user interactions, and other situations where information arrives continuously.

Error Handling

Dart uses try, catch, and finally for handling errors.

void main() {
  try {
    int result = 10 ~/ 0;
    print(result);
  } catch (error) {
    print("An error occurred");
  }
}

What Is DartPad?

DartPad is an online environment where developers can experiment with Dart code without installing a full development environment.

It can be useful for beginners who want to practice Dart syntax and small programs.

What Is Flutter?

Flutter is Google's UI toolkit for building applications from a shared codebase.

Flutter uses Dart as its primary programming language.

With Flutter, developers can build applications for multiple platforms while sharing much of the application code.

Dart and Flutter

Dart provides the programming language while Flutter provides the application UI framework and development tools.

Flutter developers use Dart to define application logic, data models, user interactions, and interface code.

import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text("Hello Flutter"),
        ),
      ),
    ),
  );
}

Dart for Android Development

Dart can be used to build Android applications through Flutter.

Flutter allows developers to create Android interfaces and application logic using Dart.

Dart for iOS Development

Dart can also be used to create iOS applications through Flutter.

This allows developers to share much of the application code between Android and iOS.

Dart for Web Development

Flutter can target the web, allowing Dart-based Flutter applications to run in web environments.

Dart can also be compiled for web-related use cases.

Dart for Desktop

Flutter supports desktop application development for supported desktop platforms.

This allows Dart developers to use Flutter to create graphical desktop applications.

Dart Packages

Packages are reusable collections of Dart code that can add functionality to a project.

Packages can provide tools for networking, databases, authentication, UI components, utilities, and many other tasks.

pub.dev

The Dart and Flutter ecosystem has a package repository called pub.dev.

Developers can search for packages, read documentation, and add useful dependencies to their projects.

What Is pubspec.yaml?

Flutter and Dart projects commonly use a file called pubspec.yaml.

This file can contain information about the project, dependencies, assets, and other configuration.

dependencies:
  flutter:
    sdk: flutter

Dart and APIs

Dart applications can communicate with APIs to retrieve or send data.

This is especially common in Flutter applications that connect to online services.

For example, a mobile app can request weather, sports, news, product, or user information from a remote API.

Dart and JSON

JSON is commonly used to exchange data between applications and APIs.

import 'dart:convert';

void main() {
  String jsonText = '{"name":"Ahmed","age":20}';

  Map<String, dynamic> data = jsonDecode(jsonText);

  print(data["name"]);
}

Dart and Databases

Dart and Flutter applications can work with local or remote databases using suitable packages and services.

Applications can store information such as users, products, messages, settings, notes, and other data.

Dart vs JavaScript

DartJavaScript
Supports static typing and type inferenceCommonly uses dynamic typing
Commonly used with FlutterCommonly used for web development
Supports null safety in its type systemUses a different type system
Can compile to different targetsRuns widely in browsers and other environments

Dart vs Kotlin

DartKotlin
Commonly used with FlutterCommonly used with Android and JVM development
Supports multi-platform development through FlutterHas a strong Android ecosystem
Uses Dart syntaxUses Kotlin syntax
Supports null safetySupports null safety

Both languages are modern and strongly typed, but they are commonly used in different development ecosystems.

Advantages of Dart

Challenges for Dart Beginners

Dart is relatively approachable, but beginners may need time to understand some concepts.

Is Dart Good for Beginners?

Yes. Dart can be a good language for beginners, especially people who want to learn Flutter and build mobile applications.

The language has readable syntax, and many concepts can be learned gradually.

Dart Learning Path

  1. Learn basic programming concepts
  2. Learn Dart syntax
  3. Learn variables and constants
  4. Learn data types
  5. Learn conditions
  6. Learn loops
  7. Learn functions
  8. Learn Lists and Maps
  9. Learn classes and objects
  10. Learn null safety
  11. Learn error handling
  12. Learn Future and async/await
  13. Learn packages
  14. Start learning Flutter
  15. Build small applications

Beginner Dart Projects

Good beginner projects include:

Common Dart Mistakes

Dart Cheat Sheet

ConceptExample
Variablevar age = 20;
StringString name = "Ahmed";
Constantconst max = 100;
Functionvoid hello() {}
List[1, 2, 3]
Map{"name": "Ahmed"}
Nullable valueString? name;
Classclass User {}
Async functionFuture<void> load() async {}
Printprint("Hello");

Frequently Asked Questions

Is Dart free?

Yes. Dart is an open-source programming language.

Is Dart the same as Flutter?

No. Dart is a programming language, while Flutter is a framework and toolkit used to build applications.

Can Dart build Android apps?

Yes. Dart can be used to build Android applications through Flutter.

Can Dart build iPhone apps?

Yes. Flutter uses Dart to create applications that can target iOS.

Can Dart build websites?

Dart can be used for web-related development, and Flutter can target the web.

Do I need Dart before Flutter?

Learning the basics of Dart before starting Flutter is strongly helpful.

Is Dart difficult to learn?

Dart has readable syntax and can be approachable for beginners. Advanced concepts such as asynchronous programming and generics require practice.

What is pub.dev?

pub.dev is the package repository used by the Dart and Flutter ecosystem.

Conclusion

Dart is a modern programming language developed by Google and is best known as the main programming language used by Flutter.

It provides variables, functions, classes, null safety, asynchronous programming, collections, packages, and many other features needed to build modern applications.

For beginners interested in mobile application development, learning Dart is a useful step toward learning Flutter.

Start with basic Dart programming, practice with small projects, and then gradually move into Flutter and complete application development.