Getting Started
4 snippetsBasic Dart syntax and fundamentals
Hello World
void main() {
print('Hello, World!');
}Comments
// Single line comment
/* Multi-line
comment */
/// Documentation commentImport
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 snippetsVariable 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 nullLate 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 snippetsArithmetic 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; // ModulusComparison
a == b // Equal
a != b // Not equal
a > b // Greater than
a < b // Less than
a >= b // Greater or equal
a <= b // Less or equalLogical Operators
a && b // AND
a || b // OR
!a // NOTNull-aware Operators
String? name;
var displayName = name ?? 'Guest'; // Default if null
name ??= 'Default'; // Assign if null
var length = name?.length; // Safe navigationCascade 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.
Control Flow
5 snippetsConditional 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 snippetsIteration 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 snippetsDefining 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); // 8Arrow 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, JaneNamed Parameters
void createUser({required String name, int age = 18}) {
print('Name: $name, Age: $age');
}
createUser(name: 'John', age: 30);
createUser(name: 'Jane'); // Uses default ageAnonymous 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 snippetsLists, 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'); // trueMaps
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 snippetsClasses 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;
}