Flutter (Dart) Cheatsheet
Some basic Dart language constructs described
Here’s a cheatsheet for Flutter (Dart) with the most useful commands and language constructs:
Above is an AI-generated image (by Flux 1 dev model) of the developer playing flute and darts.
Flutter CLI Commands
Create a new Flutter project:
flutter create <project_name>
Run a Flutter app:
flutter run
Build a release APK:
flutter build apk
Analyze Dart code:
flutter analyze
Run tests:
flutter test
Get packages:
flutter pub get
Upgrade Flutter:
flutter upgrade
Check Flutter installation:
flutter doctor
Dart Language Constructs
Variables and Data Types:
int age = 30;
double price = 9.99;
String name = 'John';
bool isActive = true;
var dynamicVar = 'Can be any type';
final constantVar = 'Cannot be reassigned';
const compiletimeConstant = 'Compile-time constant';
Null Safety:
String? nullableString;
String nonNullableString = 'Hello';
Functions:
void simplePrint() {
print('Hello');
}
int add(int a, int b) => a + b;
void namedParams({required String name, int? age}) {
// Function body
}
void optionalParams(String name, [int? age]) {
// Function body
}
Control Flow:
if (condition) {
// code
} else if (anotherCondition) {
// code
} else {
// code
}
for (var i = 0; i < 5; i++) {
// code
}
while (condition) {
// code
}
switch (variable) {
case value1:
// code
break;
default:
// code
}
Lists and Maps:
List<int> numbers = [1, 2, 3];
Map<String, int> ages = {'Alice': 30, 'Bob': 25};
Classes:
class Person {
String name;
int age;
Person(this.name, this.age);
void sayHello() {
print('Hello, I am $name');
}
}
Asynchronous Programming:
Future<void> fetchData() async {
try {
var result = await someAsyncOperation();
print(result);
} catch (e) {
print('Error: $e');
}
}
Streams:
Stream<int> countStream(int max) async* {
for (int i = 0; i < max; i++) {
yield i;
}
}
Error Handling:
try {
// Code that might throw an exception
} on SpecificException {
// Handle specific exception
} catch (e) {
// Handle any exception
} finally {
// Always executed
}
This cheatsheet covers the most essential Flutter commands and Dart language constructs, providing a quick reference for Flutter developers
Null Safety in Flutter / Dart
Handling null safety in Dart for Flutter apps is crucial for writing robust and error-free code. Here’s a simple guide on how to work with null safety:
Declaring Variables
Non-nullable variables:
int age = 30;
String name = 'John';
Nullable variables:
int? nullableAge;
String? nullableName;
Working with Nullable Types
Null-aware operators:
- Null-aware access operator (?.)
String? name;
print(name?.length); // Safely access length if name is not null
- Null-coalescing operator (??)
String? name;
String displayName = name ?? 'Guest';
- Null-aware assignment operator (??=)
String? name;
name ??= 'John'; // Assign 'John' if name is null
Null assertion operator (!) Use this operator cautiously, as it can lead to runtime errors if the value is null:
String? nullableName = 'John';
String nonNullableName = nullableName!;
Flow Analysis
Dart’s flow analysis helps determine when a nullable variable is safe to use:
String? getName() => 'John';
void printName() {
String? name = getName();
if (name != null) {
print(name.length); // Safe to use name here
}
}
Late Variables
Use late
for non-nullable variables that are initialized after declaration:
late String lateInitializedName;
void initializeName() {
lateInitializedName = 'John';
}
Handling Nullable Parameters
void greet(String name, {String? title}) {
if (title != null) {
print('Hello, $title $name');
} else {
print('Hello, $name');
}
}
Working with Collections
Nullable lists:
List<String>? nullableList;
List<String?> listOfNullableStrings = ['Hello', null, 'World'];
Filtering null values:
List<String?> mixedList = ['a', null, 'b', null, 'c'];
List<String> nonNullList = mixedList.whereType<String>().toList();
By following these practices, you can effectively handle null safety in your Dart and Flutter applications, reducing the risk of null-related errors and improving overall code quality.