๐Ÿš€ Dart Cheat Sheet โ€“ A Complete Beginner-to-Intermediate Guide

โ€ข

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, bool are common types.
  • var determines the type at compile time.
  • dynamic allows 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:

KeywordWhen Value is Assigned
constCompile time
finalRuntime

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

Your email address will not be published. Required fields are marked *