AI Generate Dart docs instantly

Dart Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Basic Dart syntax and fundamentals

Hello World

void main() {
  print('Hello, World!');
}

Comments

// Single line comment

/* Multi-line
   comment */

/// Documentation comment

Import

import 'dart:math';
import 'package:flutter/material.dart';
import 'lib/utils.dart';

Main Function

void main() {
  runApp();
}

// With arguments
void main(List<String> args) {
  print(args);
}

Variables & Types

5 snippets

Variable declaration and data types

Variable Declaration

var name = 'John';        // Type inferred
String city = 'NYC';      // Explicit type
int age = 30;
double price = 19.99;

Final & Const

final name = 'John';      // Runtime constant
const PI = 3.14159;       // Compile-time constant

final DateTime now = DateTime.now();
const int MAX = 100;

Nullable Types

String? nullableName;     // Can be null
String nonNullName = 'John';  // Cannot be null

int? age;
age ??= 30;  // Assign if null

Late Variables

late String description;

void initialize() {
  description = 'Lazy initialization';
}

Data Types

int count = 42;
double price = 19.99;
String name = 'John';
bool isActive = true;
List<int> numbers = [1, 2, 3];
Map<String, int> ages = {'John': 30};

Operators

5 snippets

Arithmetic and comparison operators

Arithmetic

var sum = 10 + 5;         // Addition
var diff = 10 - 5;        // Subtraction
var prod = 10 * 5;        // Multiplication
var quot = 10 / 5;        // Division (double)
var intDiv = 10 ~/ 3;     // Integer division
var mod = 10 % 3;         // Modulus

Comparison

a == b   // Equal
a != b   // Not equal
a > b    // Greater than
a < b    // Less than
a >= b   // Greater or equal
a <= b   // Less or equal

Logical Operators

a && b   // AND
a || b   // OR
!a       // NOT

Null-aware Operators

String? name;
var displayName = name ?? 'Guest';  // Default if null

name ??= 'Default';  // Assign if null

var length = name?.length;  // Safe navigation

Cascade Notation

var paint = Paint()
  ..color = Colors.blue
  ..strokeWidth = 5.0
  ..style = PaintingStyle.stroke;

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Control Flow

5 snippets

Conditional statements and branching

If Statement

if (age >= 18) {
  print('Adult');
}

If-Else

if (score >= 60) {
  print('Pass');
} else {
  print('Fail');
}

If-Else If

if (grade >= 90) {
  print('A');
} else if (grade >= 80) {
  print('B');
} else {
  print('C');
}

Ternary Operator

var result = age >= 18 ? 'Adult' : 'Minor';
var status = score >= 60 ? 'Pass' : 'Fail';

Switch Statement

switch (day) {
  case 'Monday':
    print('Start of week');
    break;
  case 'Friday':
    print('End of week');
    break;
  default:
    print('Midweek');
}

Loops

6 snippets

Iteration constructs

For Loop

for (var i = 0; i < 10; i++) {
  print(i);
}

For-In Loop

var fruits = ['apple', 'banana', 'cherry'];
for (var fruit in fruits) {
  print(fruit);
}

While Loop

var count = 0;
while (count < 5) {
  print(count);
  count++;
}

Do-While Loop

var i = 0;
do {
  print(i);
  i++;
} while (i < 5);

Break & Continue

for (var i = 0; i < 10; i++) {
  if (i == 5) break;
  if (i == 3) continue;
  print(i);
}

forEach Method

var numbers = [1, 2, 3, 4, 5];
numbers.forEach((num) {
  print(num);
});

Functions

6 snippets

Defining and calling functions

Basic Function

void greet(String name) {
  print('Hello, $name');
}

greet('World');

Function with Return

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

var result = add(5, 3);  // 8

Arrow Function

int multiply(int a, int b) => a * b;

String greet(String name) => 'Hello, $name';

Optional Parameters

void greet(String name, [String? greeting]) {
  print('${greeting ?? 'Hello'}, $name');
}

greet('John');           // Hello, John
greet('Jane', 'Hi');     // Hi, Jane

Named Parameters

void createUser({required String name, int age = 18}) {
  print('Name: $name, Age: $age');
}

createUser(name: 'John', age: 30);
createUser(name: 'Jane');  // Uses default age

Anonymous Functions

var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map((n) => n * 2).toList();

var sum = numbers.reduce((a, b) => a + b);

Collections

6 snippets

Lists, Sets, and Maps

Lists

var fruits = ['apple', 'banana', 'cherry'];
List<int> numbers = [1, 2, 3, 4, 5];

fruits.add('orange');
fruits.remove('banana');
print(fruits[0]);  // apple
print(fruits.length);

List Operations

var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map((n) => n * 2).toList();
var evens = numbers.where((n) => n % 2 == 0).toList();
var sum = numbers.reduce((a, b) => a + b);

Sets

var uniqueNumbers = <int>{1, 2, 3, 3, 4};
Set<String> tags = {'dart', 'flutter'};

tags.add('mobile');
tags.contains('dart');  // true

Maps

var ages = {'John': 30, 'Jane': 25};
Map<String, int> scores = {'Alice': 95, 'Bob': 87};

scores['Charlie'] = 92;
print(scores['Alice']);  // 95
scores.remove('Bob');

Spread Operator

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

var map1 = {'a': 1};
var map2 = {...map1, 'b': 2};

Collection If

var includeZero = true;
var numbers = [
  if (includeZero) 0,
  1,
  2,
  3,
];

Object-Oriented Programming

5 snippets

Classes and objects

Class Definition

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void greet() {
    print('Hello, I am $name');
  }
}

Constructor

class Person {
  String name;
  int age;

  // Default constructor
  Person(this.name, this.age);

  // Named constructor
  Person.guest()
      : name = 'Guest',
        age = 0;
}

Getters & Setters

class Rectangle {
  double width;
  double height;

  Rectangle(this.width, this.height);

  double get area => width * height;

  set dimensions(List<double> dims) {
    width = dims[0];
    height = dims[1];
  }
}

Inheritance

class Animal {
  void makeSound() {
    print('Some sound');
  }
}

class Dog extends Animal {
  @override
  void makeSound() {
    print('Woof!');
  }
}

Abstract Classes

abstract class Shape {
  double getArea();
}

class Circle extends Shape {
  double radius;

  Circle(this.radius);

  @override
  double getArea() => 3.14 * radius * radius;
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a Dart cheat sheet?

A Dart cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Dart. It helps developers quickly look up syntax without searching through documentation.

How do I learn Dart quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Dart concepts?

Key Dart concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Dart code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Dart code using AI.

Code Conversion Tools

Convert Dart to Other Languages

Easily translate your Dart code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Dart Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week