Dart Cheat Sheet [Basic] For Flutter Developers

Dart Cheat Sheet Basic

Written and reviewed by: 

SHARE

Hello Flutter Devs! In this post, I share the Basic Dart Cheat Sheet. It will help you remember the basic syntax of the Dart programming language. It will enhance your Flutter development journey.

Table of Contents

Download Dart Cheat Sheet PDF Free

Subscribe to our newsletter to download the PDF for free! We’ll send the PDF straight to your inbox.

Basic

Variable:

				
					var name = 'John'; // Inferred type
String name = 'John'; // Explicit type
final name = 'John'; // Immutable variable
const PI = 3.14; // Compile-time constant
				
			

Data Types:

				
					int age = 30;
double height = 5.9;
bool isStudent = true;
String city = 'New York';
List<int> numbers = [1, 2, 3];
Map<String, int> scores = {'Alice': 100, 'Bob': 95};

				
			

Comments:

				
					// This is a single-line comment

/* This is a 
   multi-line comment */

				
			

Printing:

				
					print('Hello, World!');
print('My name is $name and I am $age years old.');

				
			

Control flow

If-Else:

				
					if (age > 18) {
  print('Adult');
} else if (age > 12) {
  print('Teenager');
} else {
  print('Child');
}

				
			

Switch:

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

				
			

Loops:

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

// While loop
int i = 0;
while (i < 5) {
  print(i);
  i++;
}

// Do-While loop
int j = 0;
do {
  print(j);
  j++;
} while (j < 5);
				
			

Functions

Basic Functions:

				
					void sayHello() {
  print('Hello!');
}

sayHello();

				
			

Functions with Parameters:

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

greet('Alice');

				
			

Functions with Return Type:

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

int result = add(2, 3);
print(result); // 5

				
			

Anonymous Functions:

				
					var list = ['apple', 'banana', 'orange'];
list.forEach((item) {
  print(item);
});

				
			

Classes

Defining a Class:

				
					class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void introduce() {
    print('Hi, my name is $name and I am $age years old.');
  }
}

				
			

Creating an Object:

				
					var person = Person('John', 25);
person.introduce(); // Hi, my name is John and I am 25 years old.

				
			

Inheritence:

				
					class Student extends Person {
  String school;

  Student(String name, int age, this.school) : super(name, age);

  @override
  void introduce() {
    print('Hi, my name is $name, I am $age years old, and I study at $school.');
  }
}

var student = Student('Alice', 20, 'Harvard');
student.introduce();

				
			

Error Handling

Try-Catch:

				
					try {
  int result = 10 ~/ 0; // Integer division by zero
} catch (e) {
  print('Caught an error: $e');
} finally {
  print('This is always executed');
}
				
			

Asynchronous programming

Future:

				
					Future<String> fetchUserOrder() {
  return Future.delayed(Duration(seconds: 2), () => 'Cappuccino');
}

void main() {
  fetchUserOrder().then((order) {
    print('Your order is: $order');
  });
  print('Fetching your order...');
}

				
			

Async-Await:

				
					Future<void> main() async {
  print('Fetching your order...');
  var order = await fetchUserOrder();
  print('Your order is: $order');
}
				
			

Stream:

				
					Stream<int> countStream = Stream.periodic(Duration(seconds: 1), (count) => count);

void main() {
  countStream.listen((count) {
    print(count);
  });
}
				
			

Conclusion:

 

This cheatsheet covers the basics of Dart, from variables and data types to control flow, functions, classes, error handling, and asynchronous programming. With this foundation, you’re well-equipped to start exploring more advanced features and capabilities of Dart.

FAQ

Dart is primarily used for building mobile, web, and server applications. It is the language behind the Flutter framework, which is used for creating cross-platform mobile apps.

Dart uses Future and Stream classes to handle asynchronous programming. The async and await keywords are used for writing asynchronous code that looks similar to synchronous code, making it easier to read and maintain.

final is used to define a variable that can only be set once but is initialized at runtime. const is used to define a compile-time constant that must be initialized with a constant value.

Exceptions in Dart are handled using try, catch, and finally blocks. The try block contains the code that might throw an exception, the catch block handles the exception, and the finally block contains code that should always be executed regardless of whether an exception occurred.

A Future represents a potential value or error that will be available at some time in the future. It is used for asynchronous operations. You can use the then method or await keyword to handle the result of a Future.

Written and reviewed by

Picture of Muhammad Naeem
Muhammad Naeem
Professional Flutter Developer
Scroll to Top