Functions in Dart
Functions in Dart are clean and straightforward. You declare them with a return type, a name, parameters, and a body. If the function doesn't return anything, use void. Dart also supports a nice arrow syntax for one-liners that keeps your code concise.
// Regular function
int add(int a, int b) {
return a + b;
}
// Arrow syntax (one-liner)
int multiply(int a, int b) => a * b;
// Function with optional parameters
String greet(String name, {String greeting = 'Hello'}) {
return '$greeting, $name!';
}
void main() {
print(add(5, 3)); // 8
print(multiply(4, 2)); // 8
print(greet('Alice')); // Hello, Alice!
print(greet('Bob', greeting: 'Hi')); // Hi, Bob!
}
The arrow syntax is perfect for simple functions. It's clean, readable, and reduces boilerplate. You'll see it everywhere in Flutter code.
Try it Yourself →Named Parameters and Required
One of Dart's nicest features is named parameters. Instead of passing arguments by position, you pass them by name using curly braces. This makes your code much more readable, especially when a function has many parameters.
class UserProfile {
String name;
int age;
String email;
UserProfile({
required this.name,
required this.age,
this.email = '',
});
void display() {
print('Name: $name, Age: $age, Email: $email');
}
}
void main() {
var user = UserProfile(
name: 'Alice',
age: 30,
email: 'alice@example.com',
);
user.display();
}
The required keyword makes sure you don't forget important parameters. Optional parameters without required need a default value. This pattern is used everywhere in Flutter — you'll see it in every widget constructor.
Classes and Objects
Classes in Dart work like Java or C#. Use the class keyword, create constructors, define methods. Dart supports named constructors, factory constructors, and getter/setter properties. It's object-oriented programming done right.
class Animal {
String name;
int legs;
Animal(this.name, this.legs); // Shorthand constructor
void describe() {
print('$name has $legs legs');
}
}
class Dog extends Animal {
String breed;
Dog(String name, this.breed) : super(name, 4);
@override
void describe() {
super.describe();
print('Breed: $breed');
}
}
void main() {
var dog = Dog('Rex', 'German Shepherd');
dog.describe();
// Rex has 4 legs
// Breed: German Shepherd
}
Try it Yourself →
Mixins and Enums
Mixins let you share behavior across classes without using inheritance. Think of them as plug-and-play functionality. Enums define fixed sets of constants — perfect for representing states or categories in your app.
// Mixin
mixin Swimmable {
void swim() => print('Swimming...');
}
mixin Flyable {
void fly() => print('Flying...');
}
class Duck with Swimmable, Flyable {
String name;
Duck(this.name);
}
// Enum
enum Color { red, green, blue }
enum TaskStatus { pending, inProgress, completed }
void main() {
var duck = Duck('Donald');
duck.swim(); // Swimming...
duck.fly(); // Flying...
var currentColor = Color.blue;
if (currentColor == Color.blue) {
print('The color is blue');
}
var status = TaskStatus.completed;
switch (status) {
case TaskStatus.pending:
print('Task is pending');
case TaskStatus.inProgress:
print('Task is in progress');
case TaskStatus.completed:
print('Task is done!');
}
}
Try it Yourself →
Async/Await
Modern apps are full of asynchronous operations — fetching data from APIs, reading files, waiting for user input. Dart handles this beautifully with async/await, just like JavaScript. Mark a function as async and use await to pause execution until a Future completes.
import 'dart:async';
Future fetchUserData() async {
// Simulate network delay
await Future.delayed(Duration(seconds: 2));
return 'User: Alice, Score: 95';
}
Future main() async {
print('Loading...');
var data = await fetchUserData();
print(data);
// Parallel execution
var results = await Future.wait([
fetchUserData(),
Future.delayed(Duration(seconds: 1), () => 'Extra data'),
]);
print('Results: $results');
}
The await keyword pauses the function until the Future resolves. The async keyword tells Dart this function might do async work. These are the building blocks you'll use in every Flutter app that talks to the internet or does any background work.
Try it Yourself →