Dart

Dart is a modern, object-oriented programming language developed by Google. It is designed for building high-performance web, mobile, and desktop applications. Dart can be compiled both to native machine code and to JavaScript, making it versatile for different deployment targets. It offers features like strong typing, garbage collection, and asynchronous programming support. Dart's syntax is similar to languages like C++ and Java, making it approachable for developers from various backgrounds. Flutter, a popular cross-platform mobile development framework, utilizes Dart as its primary language, enabling developers to build native mobile apps for iOS and Android from a single codebase.

Module 1: Introduction to Dart

Introduction to Dart

Dart is a client-optimized programming language for fast apps on multiple platforms. It is developed by Google and is used to build mobile, desktop, server, and web applications. Dart is an object-oriented, class-based, garbage-collected language with C-style syntax.

Variables, Types and Control Flow

Dart is a statically typed language, which means that it enforces type safety. You can define variables using the 'var' keyword for inferred typing, or explicitly declare the type. Dart has built-in support for common data types, like 'int', 'double', 'bool', 'String', and more. Dart's control flow statements include standard constructs like 'if', 'else', 'for', 'while', 'switch', and others.

var name = 'Dart';
int year = 2023;
if (year > 2020) {
	print('Future year');
}

Functions

Functions in Dart are defined using the 'void' keyword for functions that don't return a value, or the specific return type for functions that do. Dart supports optional parameters and default parameter values.

void greet(String name, [String greeting = 'Hello']) {
	print('greeting + ' ' + name);
}
greet('Dart');

Module 2: Object-Oriented Programming in Dart

Classes and Objects

Dart is an object-oriented language and supports classes and objects. A class can be defined using the 'class' keyword and can contain fields (variables) and methods (functions). You can create an instance of a class (an object) using the 'new' keyword (which is optional).

class Car {
	String brand;
	int year;
	Car(this.brand, this.year);
}
var car = Car('Toyota', 2023);

Inheritance and Interfaces

Dart supports single inheritance, meaning that each class (except for the base 'Object' class) has exactly one superclass. Dart also supports interfaces implicitly, meaning that every class defines an interface comprising all its instance members.

class Vehicle {
	int maxSpeed;
}
class Car extends Vehicle {
	String brand;
	int year;
	Car(this.brand, this.year, this.maxSpeed);
}

Module 3: Collections and Generics

Dart Collections

Dart provides built-in support for collections like Lists (arrays), Sets, and Maps. Lists are ordered groups of items, Sets are unordered collections of unique items, and Maps store pairs of keys and values.

var list = [1, 2, 3];
var set = {1, 2, 3};
var map = {'key': 'value'};

Generics

Generics are a way to create reusable code while maintaining type safety. You can use generics to specify the type of elements in a collection, the type of a function's arguments and return value, and more.

List<String> names = ['Alice', 'Bob', 'Charlie'];

Module 4: Asynchronous Programming

Futures and Async/Await

Dart provides Future and Stream classes to handle asynchronous operations. A Future represents a potential value or error that will be available at some time in the future. The 'async' and 'await' keywords provide a declarative way to define asynchronous functions and use their results.

Future<void> fetchUserOrder() async {
	return Future.delayed(Duration(seconds: 2), () => print('Large Latte'));
}
await fetchUserOrder();

Streams

A Stream provides a way to receive a sequence of events. Each event is either a data event, representing a value, or an error event, representing an error. A Stream can be listened to, and it delivers its events when they are ready.

Stream.fromIterable([1, 2, 3, 4, 5]).listen(print);

Module 5: Testing and Error Handling

Writing and Running Tests

Dart provides a powerful testing library to write tests for your code. You can write unit tests (tests for a single function or method), widget tests (tests for a single widget), and integration tests (tests for the complete app or a large part of an app).

Error Handling

Dart provides 'try-catch' blocks to handle exceptions and prevent them from propagating. You can catch specific types of exceptions, or catch all exceptions. You can also throw exceptions using the 'throw' statement.

try {
	int result = 12 ~/ 0;
} catch(e) {
	print('Exception: $e');
}