Dart is a modern, object-oriented programming language developed by Google and is primarily used for building Flutter applications. This cheat sheet gives you a clear, structured, and in-depth understanding of Dartโs core concepts, exactly like the screenshot โ but with real explanations and examples.
๐น 1. Variables in Dart
Dart supports static typing, but it also provides type inference.
Example:
int n1 = 5; // explicitly typed
var n2 = 4; // type inferred as int
dynamic n3 = "abc"; // can change type later
Key Points:
int,double,String,boolare common types.vardetermines the type at compile time.dynamicallows type changes at runtime (use carefully).
n3 = 10; // Valid
๐น 2. Constants (final vs const)
const PI = 3.14; // compile-time constant
final area = PI * 5 * 5; // runtime constant
Difference:
| Keyword | When Value is Assigned |
|---|---|
const | Compile time |
final | Runtime |
Use const when value never changes and is known beforehand.
๐น 3. Conditional Expressions
Ternary Operator
var grade = 3;
var result = grade > 3 ? "Cool" : "Not cool";
Null Coalescing Operator
var input;
var age = input ?? 0;
โ Assigns default value if input is null.
๐น 4. Functions
Standard Function
int addNums(int a, int b, int c) {
return a + b + c;
}
Calling a Function
print(addNums(1, 2, 3));
๐น 5. Arrow Syntax (Short Functions)
Used for single-expression functions.
void greet() => print("Hello!");
This is cleaner and easier to read.
๐น 6. Optional Positional Parameters
Use square brackets [].
int add(int a, [int b = 0, int c = 0]) {
return a + b + c;
}
add(1);
add(1, 2);
add(1, 2, 3);
๐น 7. Named Parameters
Named parameters improve readability.
int add({int a = 0, int b = 0, int c = 0}) {
return a + b + c;
}
add(a: 1, b: 2);
๐น 8. String Interpolation
var name = "World";
print("Hello $name");
print("Sum of 5 and 6 is ${5 + 6}");
โ Cleaner than string concatenation.
๐น 9. Parsing Strings
int.parse("123"); // 123
double.parse("12.5"); // 12.5
Safe parsing:
int.tryParse("abc"); // null
๐น 10. Lists (Arrays)
var arr = [1, 2, 3, 4, 5];
print(arr.length); // 5
arr[0] = 10;
Add / Remove
arr.add(6);
arr.removeAt(2);
๐น 11. Fixed vs Growable Lists
var fixed = List.filled(3, null);
Fixed-length lists cannot grow.
๐น 12. Maps (KeyโValue Pairs)
var person = {
"name": "Sam",
"age": 40
};
Looping through a map:
person.forEach((key, value) {
print("$key: $value");
});
๐น 13. Lambda Functions
var nums = [1, 2, 3, 4, 5];
var evens = nums.where((n) => n % 2 == 0).toList();
๐น 14. Higher-Order Functions
Functions that accept other functions.
nums.forEach((n) => print(n));
๐น 15. Sorting Lists
Ascending
nums.sort((a, b) => a.compareTo(b));
Descending
nums.sort((a, b) => b.compareTo(a));
๐น 16. Custom Sort Example (Bubble Sort)
List bubbleSort(List items) {
for (var i = 0; i < items.length; i++) {
for (var j = 0; j < items.length - 1; j++) {
if (items[j] > items[j + 1]) {
var temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
}
}
}
return items;
}
โ Final Thoughts
This Dart cheat sheet covers:
- Core syntax
- Functional programming concepts
- Collections
- Real-world usage patterns
Perfect for:
โ
Flutter developers
โ
Interview prep
โ
Quick revision


Leave a Reply