Home
Flutter
Dart Basics: Top Concepts
August 13, 2026
1 min

Table Of Contents

01
1. var, final, const, and late
02
2. Null Safety
03
3. Functions
04
4. Lists, Sets, and Maps
05
5. Collection methods
06
6. Classes and Objects

Dart is the language behind Flutter. These are the core concepts you need before building widgets.

1. var, final, const, and late

Dart is statically typed. You can write the type yourself (String name = "Daniel") or let Dart infer it with var. Common types are int, double, String, bool, List, and Map.

How you declare a variable controls whether it can change.

String name = "Daniel"; // explicit type
var count = 0; // inferred as int, can be reassigned
final createdAt = DateTime.now(); // set once at runtime
const pi = 3.14159; // compile-time constant
late String token; // assigned later, before first use
token = "abc123";

Rules:

  • var → mutable, type is inferred
  • final → assigned once, value can be computed at runtime
  • const → compile-time constant
  • late → delayed initialization (useful for non-nullable fields)

2. Null Safety

By default, variables cannot be null. Add ? to allow it.

String title = "Hello";
String? subtitle; // can be null
print(subtitle ?? "No subtitle");
print(subtitle?.length);
subtitle ??= "Default";

Important operators:

  • ? → nullable type
  • ?? → fallback if null
  • ?. → call only if not null
  • ??= → assign only if currently null
  • ! → assert the value is not null

3. Functions

String greet(String name) {
return "Hello $name";
}
int add(int a, int b) => a + b;
void log(String message, {String level = "info"}) {
print("[$level] $message");
}
log("App started");
log("Crash", level: "error");

Mark a function async to use await. It returns a Future.

Future<String> fetchUser() async {
await Future.delayed(Duration(seconds: 1));
return "Daniel";
}
void main() async {
final name = await fetchUser();
print("Hello $name");
}

Function styles:

  • Positional parameters: greet("Daniel")
  • Named parameters: {String level = "info"}
  • Required named: {required String name}
  • Arrow functions: => for one-line returns
  • async / await → wait for a Future without blocking

4. Lists, Sets, and Maps

List

final fruits = ["apple", "banana", "orange"];
print(fruits[0]);
fruits.add("mango");

Set

final tags = {"dart", "flutter", "dart"};
print(tags.length); // 2 — duplicates are removed

Map

final user = {
"name": "Daniel",
"age": 25,
};
print(user["name"]);
user["city"] = "Hanoi";

5. Collection methods

These methods work on List, Set, and other iterables. map and where return an Iterable — call .toList() when you need a list.

final numbers = [1, 2, 3, 4, 5];
final fruits = ["apple", "banana", "orange"];

map()

Transforms each item into something new.

final doubled = numbers.map((n) => n * 2).toList();
// [2, 4, 6, 8, 10]

where()

Keeps only items that match a condition.

final evens = numbers.where((n) => n.isEven).toList();
// [2, 4]

firstWhere()

Returns the first item that matches. Use orElse if nothing matches.

final firstEven = numbers.firstWhere((n) => n.isEven);
// 2
final big = numbers.firstWhere(
(n) => n > 10,
orElse: () => -1,
);

any()

Returns true if at least one item matches.

numbers.any((n) => n > 4); // true

every()

Returns true if every item matches.

numbers.every((n) => n > 0); // true

contains()

Checks whether a value exists in the collection.

fruits.contains("banana"); // true

forEach()

Runs a function on each item.

fruits.forEach((fruit) => print(fruit));

6. Classes and Objects

class Dog {
final String name;
Dog(this.name);
void bark() {
print("Woof!");
}
}
final dog = Dog("Buddy");
dog.bark();

Useful class features:

  • Constructor: Dog(this.name)
  • Named constructor: Dog.stray() : name = "Unknown"
  • Getters and setters
  • this refers to the current instance
class User {
final String name;
final int age;
User({required this.name, required this.age});
factory User.guest() {
return User(name: "Guest", age: 0);
}
String get label => "$name ($age)";
}

Tags

#Flutter#Dart

Share

© 2026, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube