Learning Dart with Code Examples
Introduction to Dart:
- Dart is a programming language designed for client-side development, especially for building
mobile applications with Flutter.
- It is optimized for performance and scalability.
- In this guide, we'll walk through Dart concepts with examples to help you understand how to use
them effectively.
Basic Dart Concepts:
1. Variables and Data Types:
Dart has several data types such as int, double, String, bool, and more.
Example:
```dart
int age = 25;
double height = 5.9;
String name = "John Doe";
bool isStudent = true;
print(name); // Output: John Doe
```
2. Constants and Final Variables:
Use 'final' for runtime constants and 'const' for compile-time constants.
Example:
```dart
final String city = "Cape Town";
const double pi = 3.14159;
print(city); // Output: Cape Town
```
3. Operators:
Dart supports basic arithmetic, relational, and logical operators.
Example:
```dart
int x = 10, y = 5;
print(x + y); // Output: 15
print(x > y); // Output: true
```
4. Control Flow:
Dart supports conditional statements like if, else, and switch.
Example:
```dart
if (age >= 18) {
print("Adult");
} else {
print("Minor");
```
Functions in Dart:
1. Declaring Functions:
Dart allows you to declare functions with or without parameters.
Example:
```dart
void greet(String name) {
print("Hello, $name!");
greet("Alice"); // Output: Hello, Alice!
```
2. Return Types and Function Expressions:
You can also return values from functions.
Example:
```dart
int add(int a, int b) {
return a + b;
print(add(5, 7)); // Output: 12
```
3. Arrow Functions:
Dart supports concise functions using the '=> syntax'.
Example:
```dart
int multiply(int a, int b) => a * b;
print(multiply(4, 6)); // Output: 24
```
Object-Oriented Programming (OOP) in Dart:
1. Creating Classes and Objects:
Dart is object-oriented and allows the creation of classes and objects.
Example:
```dart
class Person {
String name;
int age;
Person(this.name, this.age);
void introduce() {
print("Hi, I'm $name and I'm $age years old.");
var person = Person("Alice", 30);
person.introduce(); // Output: Hi, I'm Alice and I'm 30 years old.
```
2. Inheritance and Overriding:
Dart supports inheritance and method overriding.
Example:
```dart
class Animal {
void speak() {
print("Animal speaks");
class Dog extends Animal {
@override
void speak() {
print("Dog barks");
var dog = Dog();
dog.speak(); // Output: Dog barks
```
Dart Collections:
1. Lists:
Dart provides a List collection to store ordered items.
Example:
```dart
var fruits = ["Apple", "Banana", "Orange"];
fruits.add("Mango");
print(fruits); // Output: [Apple, Banana, Orange, Mango]
```
2. Maps:
Dart supports key-value pairs using Maps.
Example:
```dart
var user = {"name": "Alice", "age": 30};
print(user["name"]); // Output: Alice
```
Asynchronous Programming:
1. Futures and async/await:
Dart allows you to write asynchronous code using Futures, async, and await.
Example:
```dart
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return "Data loaded";
void main() async {
var result = await fetchData();
print(result); // Output: Data loaded
```
Error Handling:
1. Try, Catch, Finally:
Dart provides error handling using try-catch-finally blocks.
Example:
```dart
try {
int result = 10 ~/ 0;
} catch (e) {
print("Error: $e"); // Output: Error: IntegerDivisionByZeroException
} finally {
print("Execution completed");
```
Advanced Dart Features:
1. Generics:
Dart supports generics to provide type safety in collections and methods.
Example:
```dart
class Box<T> {
T value;
Box(this.value);
var intBox = Box<int>(100);
print(intBox.value); // Output: 100
```
2. Extension Methods:
Dart allows you to extend classes without modifying their original code.
Example:
```dart
extension StringReversal on String {
String get reversed {
return this.split('').reversed.join('');
var name = "Dart";
print(name.reversed); // Output: traD
```