Dart Programming Language

Introduction to Dart

What is Dart?

  • Dart is an open-source, general-purpose programming language developed by Google.
  • It’s designed for building web, server, desktop, and mobile applications.
  • Often used with Flutter for developing cross-platform mobile apps.

Key Features:

  • Statically Typed: Dart supports both strong and dynamic typing, allowing flexibility.
  • Optimized for UI Development: Designed with a focus on creating front-end interfaces, especially for mobile apps.
  • Compiles to Native Code: Can compile to ARM and x64 machine code, enabling high-performance applications.

Dart Development Environment

Installing Dart SDK:

  • Download and install Dart SDK from the official website (https://dart.dev).
  • Install Dart plugins for your IDE (e.g., VS Code, IntelliJ IDEA).

Basic Tools:

  • Dart CLI: Command-line interface for running Dart programs (dart run), compiling (dart compile), and testing.
  • Pub: Dart’s package manager for adding, updating, and managing external libraries.

Syntax and Basics of Dart

Hello World Program:

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

Variables and Data Types:

  • Dart is statically typed but allows type inference:
var name = 'Dart'; // Inferred as String
int age = 20;
double height = 5.9;
bool isStudent = true;
  • Const and Final for immutable variables.

Control Flow Statements:

  • If-Else:
if (age > 18) {
  print('Adult');
} else {
  print('Not an adult');
}
  • Switch-Case:
switch (age) {
  case 18:
    print('Just an adult');
    break;
  default:
    print('Age is $age');
}

Functions and Parameters

Defining Functions:

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

Optional Parameters:

  • Named parameters:
void greet({String name = 'Guest'}) {
  print('Hello, $name');
}
  • Positional parameters:
void describe(String name, [int age = 18]) {
  print('$name is $age years old');
}

Object-Oriented Programming in Dart

Classes and Objects:

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void display() {
    print('$name is $age years old');
  }
}

Inheritance:

  • Classes can inherit from other classes:
class Student extends Person {
  int studentID;

  Student(String name, int age, this.studentID) : super(name, age);
}

Collections in Dart

Lists (Arrays):

List<int> numbers = [1, 2, 3, 4, 5];

Sets:

Set<String> names = {'Alice', 'Bob', 'Charlie'};

Maps (Dictionaries):

Map<String, int> scores = {'Alice': 90, 'Bob': 85};

Dart Libraries and Packages

Using Libraries:

  • Dart has built-in libraries like dart:core, dart:async, and dart:io.
  • Importing libraries:
import 'dart:math';

Adding External Packages:

  • Dart uses pub.dev to manage packages.
dependencies:
  http: ^0.13.0

Asynchronous Programming in Dart

Futures:

Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  return 'Data fetched';
}

Async and Await:

  • Dart uses async/await for handling asynchronous operations:
void main() async {
  String data = await fetchData();
  print(data);
}

Dart for Flutter Development

  • Dart is the primary language used with Flutter for building cross-platform mobile applications.
  • Hot Reload Feature: Boosts productivity by allowing live changes to the code without restarting the app.
  • Strong integration with UI development through widgets and state management.

Best Practices in Dart

  • Use Effective Naming: Consistent and meaningful variable and function names.
  • Follow Style Guidelines: Dart has a set of style guidelines (e.g., using camelCase for variables and methods).
  • Avoid Global State: Encapsulate data within classes and manage state effectively.

Leave a Reply

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