Sunday, November 23, 2025

Dart Programming Language Cheatsheet – A Complete Guide

 


Dart Programming Language Cheatsheet – A Complete Guide

Dart Programming


Dart is a modern, fast, and expressive programming language designed by Google. It is best known today as the core language behind Flutter, enabling developers to build cross-platform applications for Android, iOS, Web, Desktop, and Embedded systems—all from a single codebase. While Dart is easy to pick up, having a quick reference guide or “cheatsheet” helps developers write clean, efficient, and consistent code.

This comprehensive cheatsheet offers everything you need to get started with Dart—covering syntax, variables, data types, functions, classes, async programming, collections, and more. Whether you're a beginner or brushing up on essential concepts, this Dart cheatsheet will act as your go-to quick reference.

1. Introduction to Dart

Dart is an object-oriented, statically typed language with a clean syntax. It supports:

  • AOT (Ahead-of-Time) compilation for fast, native apps.
  • JIT (Just-in-Time) compilation for fast development cycles with hot reload.
  • Sound type system, meaning variable types are known at compile time.
  • Asynchronous programming, making it ideal for UI and event-driven apps.

2. Basic Syntax Cheatsheet

2.1 Main Function

Every Dart application starts with the main() method.

void main() {
  print("Hello, Dart!");
}

2.2 Comments

Dart supports single, multi-line, and documentation comments:

// Single-line comment
/* Multi-line
   comment */
/** Documentation comment */

3. Variables and Data Types

3.1 Declaring Variables

var name = "Dart";     // Type inferred
String language = "Dart";
int age = 10;
double rating = 4.8;
bool isOpen = true;

3.2 Final and Const

final city = "Mumbai";  // Runtime constant
const PI = 3.14;         // Compile-time constant

3.3 Nullable and Non-Nullable Types

int a = 10;       // Non-nullable
int? b;           // Nullable

4. Operators Cheatsheet

4.1 Arithmetic Operators

+ - * / % ~/

4.2 Assignment Operators

= += -= *= /= %=

4.3 Comparison Operators

== != > < >= <=

4.4 Logical Operators

&& || !

4.5 Type Test Operators

is is! as

Example:

if (name is String) {
  print("Name is a string");
}

5. Control Flow Statements

5.1 If–Else

if (age > 18) {
  print("Adult");
} else {
  print("Minor");
}

5.2 Switch Case

switch (day) {
  case "Mon":
    print("Start of week");
    break;
  default:
    print("Other day");
}

5.3 Loops

For Loop

for (int i = 0; i < 5; i++) {
  print(i);
}

While Loop

while (count < 5) {
  count++;
}

Do-While Loop

do {
  print("Running...");
} while (isRunning);

6. Functions Cheatsheet

6.1 Function Declaration

void greet() {
  print("Hello!");
}

6.2 Function With Return Type

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

6.3 Arrow Functions

int square(int n) => n * n;

6.4 Optional Parameters

Optional Positional

void info(String name, [String? city]) {}

Named Optional

void details({String? name, int? age}) {}

Named Required

void register({required String email, required String password}) {}

7. Collections Cheatsheet

Dart has powerful collection types.

7.1 Lists

List<int> nums = [1, 2, 3];
nums.add(4);

Constant list:

const fixed = [1, 2, 3];

7.2 Sets

Set<String> fruits = {"Apple", "Banana"};
fruits.add("Mango");

7.3 Maps

Map<String, int> ages = {
  "John": 25,
  "Rita": 30
};
print(ages["John"]);

7.4 Spread Operator

var list1 = [1, 2];
var list2 = [...list1, 3, 4];

7.5 Null-Aware Spread

List<int>? items;
var newList = [...?items, 10];

8. String Cheatsheet

8.1 Multi-Line Strings

var text = '''
This is a
multi-line string.
''';

8.2 String Interpolation

print("Name: $name, Age: ${age + 1}");

8.3 Common String Methods

  • length
  • toUpperCase()
  • toLowerCase()
  • contains()
  • trim()
  • split()

Example:

print(name.toUpperCase());

L9. Classes and Objects

9.1 Basic Class

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void show() {
    print("$name is $age years old");
  }
}

9.2 Named Constructors

class Point {
  int x, y;
  Point.origin() : x = 0, y = 0;
}

9.3 Inheritance

class Animal {
  void speak() => print("Animal sound");
}

class Dog extends Animal {
  @override
  void speak() => print("Bark!");
}

9.4 Abstract Classes

abstract class Shape {
  void draw();
}

9.5 Mixins

mixin Fly {
  void fly() => print("Flying!");
}

class Bird with Fly {}

10. Async and Await Cheatsheet

Dart is built with asynchronous programming at its core.

10.1 Future Example

Future<String> fetchData() async {
  return "Data loaded";
}

10.2 Using Async/Await

void main() async {
  var data = await fetchData();
  print(data);
}

10.3 Streams

Stream<int> numbers() async* {
  for (int i = 1; i <= 5; i++) {
    yield i;
  }
}

11. Exception Handling

Try–Catch–Finally

try {
  var result = 10 ~/ 0;
} catch (e) {
  print("Error: $e");
} finally {
  print("Done");
}

12. Important Dart Keywords

Some essential keywords include:

Keyword Purpose
var Type inference
final Runtime constant
const Compile-time constant
late Initialize later
async Asynchronous function
await Wait for Future
yield Produce stream values
enum Enum types

13. Dart Tips and Best Practices

13.1 Prefer final over var

Use final to avoid unintentional reassignment.

13.2 Use Meaningful Variable Names

Improves code readability.

13.3 Use Null-Aware Operators

E.g., ??, ??=, ?.

var value = name ?? "Guest";

13.4 Always Handle Errors

Use try-catch around risky code.

13.5 Write Clean Classes

Keep methods short and maintain encapsulation.

14. Conclusion

This Dart cheatsheet provides a complete reference to help developers quickly recall essential Dart syntax, concepts, and patterns. Whether you're building mobile apps with Flutter or writing standalone Dart programs, having a compact guide like this makes coding smoother and faster. From basic syntax to classes, async programming, and collections, this cheatsheet equips you with the core knowledge needed for everyday Dart development.

If you continue practicing these concepts and writing small programs, mastering Dart becomes not only easier but enjoyable. Use this guide as your handy companion whenever you're working with Dart, and you’ll see your productivity and confidence continue to grow.