Dart is a client-optimized programming language for fast apps on any platform. It is developed by Google and is used to build mobile, desktop, server, and web applications. Dart is the language behind Flutter, which is a popular framework for building natively compiled applications for mobile, web, and desktop from a single codebase.

Key Features of Dart

  • Optimized for UI: Dart is designed to create smooth and high-performance user interfaces.
  • Productive Development: Dart offers a rich set of libraries and tools that make development faster and more efficient.
  • Fast on All Platforms: Dart compiles to ARM and x64 machine code for mobile, desktop, and backend. For web, Dart compiles to JavaScript.
  • Strongly Typed: Dart is a statically typed language, which means that type checking is done at compile time.

Setting Up Dart

Before you start coding in Dart, you need to set up your development environment. Here’s how you can do it:

  1. Install Dart SDK:

    • Visit the Dart SDK page and follow the instructions for your operating system.
    • Verify the installation by running dart --version in your terminal.
  2. Install an IDE:

    • You can use any text editor or IDE, but Visual Studio Code and IntelliJ IDEA are popular choices.
    • Install the Dart plugin for your chosen IDE to get features like syntax highlighting, code completion, and debugging.

Your First Dart Program

Let's start with a simple "Hello, World!" program in Dart.

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

Explanation

  • void main() { ... }: This is the main function, the entry point of every Dart application.
  • print('Hello, World!');: This line prints the string 'Hello, World!' to the console.

Dart Syntax Basics

Variables

In Dart, you can declare variables using var, final, or const.

void main() {
  var name = 'John'; // Variable that can change
  final age = 30; // Variable that cannot change
  const pi = 3.14; // Compile-time constant
}

Data Types

Dart supports various data types, including:

  • Numbers: int, double
  • Strings: String
  • Booleans: bool
  • Lists: List
  • Maps: Map

Example

void main() {
  int age = 25;
  double height = 5.9;
  String name = 'Alice';
  bool isStudent = true;
  List<String> colors = ['red', 'green', 'blue'];
  Map<String, int> scores = {'math': 90, 'science': 85};

  print('Name: $name');
  print('Age: $age');
  print('Height: $height');
  print('Is Student: $isStudent');
  print('Colors: $colors');
  print('Scores: $scores');
}

Control Flow Statements

Dart supports standard control flow statements like if, else, for, while, and switch.

Example

void main() {
  int score = 85;

  if (score >= 90) {
    print('Grade: A');
  } else if (score >= 80) {
    print('Grade: B');
  } else {
    print('Grade: C');
  }

  for (int i = 0; i < 5; i++) {
    print('i: $i');
  }

  int count = 0;
  while (count < 3) {
    print('count: $count');
    count++;
  }

  switch (score) {
    case 90:
      print('Excellent');
      break;
    case 85:
      print('Very Good');
      break;
    default:
      print('Good');
  }
}

Practical Exercise

Task

Write a Dart program that calculates the area of a circle. The program should prompt the user to enter the radius and then display the calculated area.

Solution

import 'dart:io';

void main() {
  print('Enter the radius of the circle:');
  String? input = stdin.readLineSync();
  double radius = double.parse(input!);

  double area = 3.14 * radius * radius;
  print('The area of the circle is: $area');
}

Explanation

  • import 'dart:io';: This imports the dart:io library, which is used for input and output operations.
  • String? input = stdin.readLineSync();: This reads a line of text from the console.
  • double radius = double.parse(input!);: This converts the input string to a double.
  • double area = 3.14 * radius * radius;: This calculates the area of the circle.
  • print('The area of the circle is: $area');: This prints the calculated area to the console.

Summary

In this section, you learned the basics of Dart, including its key features, how to set up the development environment, and basic syntax. You also wrote your first Dart program and learned about variables, data types, and control flow statements. Finally, you completed a practical exercise to reinforce your understanding. In the next section, we will dive deeper into variables and data types in Dart.

Flutter Development Course

Module 1: Introduction to Flutter

Module 2: Dart Programming Basics

Module 3: Flutter Widgets

Module 4: State Management

Module 5: Navigation and Routing

Module 6: Networking and APIs

Module 7: Persistence and Storage

Module 8: Advanced Flutter Concepts

Module 9: Testing and Debugging

Module 10: Deployment and Maintenance

Module 11: Flutter for Web and Desktop

© Copyright 2024. All rights reserved