Dart Cheatsheet
50 essential methods for coding interviews
Showing 50 of 50 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
add / addAll | list.add(element) / list.addAll(iterable) | Adds element(s) to the end of the list | O(1) for add, O(n) for addAll | essential |
remove / removeAt | list.remove(element) / list.removeAt(index) | Removes first occurrence of element or element at index | O(n) | essential |
removeLast | list.removeLast() | Removes and returns the last element | O(1) | essential |
indexOf | list.indexOf(element, [start]) | Returns the first index of element, or -1 if not found | O(n) | essential |
contains | list.contains(element) | Returns true if the list contains the element | O(n) | essential |
sublist | list.sublist(start, [end]) | Returns a new list from start to end (exclusive) | O(n) | essential |
reversed | list.reversed | Returns an iterable of elements in reverse order | O(1) for iterable, O(n) for toList() | essential |
sort | list.sort([compare]) | Sorts the list in-place using optional comparator | O(n log n) | essential |
first / last | list.first / list.last | Gets the first or last element of the list | O(1) | essential |
isEmpty / isNotEmpty / length | list.isEmpty / list.isNotEmpty / list.length | Checks if list is empty or gets its length | O(1) | essential |
List.filled | List.filled(length, fill, {growable: true}) | Creates a list of given length filled with a value | O(n) | essential |
List.generate | List.generate(length, (index) => value) | Creates a list by calling generator for each index | O(n) | essential |
map | iterable.map((e) => transform) | Transforms each element and returns a lazy iterable | O(n) | essential |
where | iterable.where((e) => condition) | Filters elements matching a condition (like filter) | O(n) | essential |
fold | iterable.fold(initial, (acc, e) => combine) | Reduces elements to a single value with initial value | O(n) | essential |
reduce | iterable.reduce((acc, e) => combine) | Reduces elements using first element as initial value | O(n) | essential |
firstWhere | iterable.firstWhere((e) => condition, {orElse: () => default}) | Returns first element matching condition | O(n) | essential |
split | str.split(pattern) | Splits string by pattern into a list of substrings | O(n) | essential |
substring | str.substring(start, [end]) | Returns substring from start to end (exclusive) | O(n) | essential |
contains (String) | str.contains(pattern) | Returns true if string contains the pattern | O(n*m) | essential |
startsWith / endsWith | str.startsWith(prefix) / str.endsWith(suffix) | Checks if string starts or ends with given pattern | O(m) where m is pattern length | essential |
indexOf / lastIndexOf (String) | str.indexOf(pattern) / str.lastIndexOf(pattern) | Returns index of first/last occurrence, or -1 | O(n*m) | essential |
replaceAll / replaceFirst | str.replaceAll(from, to) | Replaces all or first occurrence of pattern | O(n) | essential |
trim | str.trim() / str.trimLeft() / str.trimRight() | Removes leading and/or trailing whitespace | O(n) | essential |
toUpperCase / toLowerCase | str.toUpperCase() / str.toLowerCase() | Converts string to upper or lower case | O(n) | essential |
Map literal / Map() | var map = {'key': value} / Map<K, V>() | Creates a new map (hash map by default) | O(n) | essential |
putIfAbsent | map.putIfAbsent(key, () => value) | Adds key-value only if key is not present | O(1) average | essential |
update | map.update(key, (v) => newV, {ifAbsent: () => default}) | Updates value for key using update function | O(1) average | essential |
containsKey / containsValue | map.containsKey(key) / map.containsValue(value) | Checks if map contains key or value | O(1) for key, O(n) for value | essential |
keys / values / entries | map.keys / map.values / map.entries | Gets iterables of keys, values, or key-value pairs | O(1) for property, O(n) for iteration | essential |
remove (Map) | map.remove(key) | Removes key and returns its value, or null | O(1) average | essential |
Set literal / Set() | var set = {1, 2, 3} / Set<T>() | Creates a new set (hash set by default) | O(n) | essential |
add / remove (Set) | set.add(element) / set.remove(element) | Adds or removes an element from set | O(1) average | essential |
union / intersection / difference | set1.union(set2) / set1.intersection(set2) / set1.difference(set2) | Set operations returning new sets | O(n) | essential |
toSet | iterable.toSet() | Converts any iterable to a Set, removing duplicates | O(n) | essential |
for-in loop | for (var item in iterable) { ... } | Iterates over elements of any iterable | O(n) | essential |
int.parse / double.parse | int.parse(str) / double.parse(str) | Parses string to int or double | O(n) | essential |
toString | value.toString() | Converts any value to its string representation | O(n) | essential |
toList / toSet | iterable.toList() / iterable.toSet() | Converts iterable to List or Set | O(n) | essential |
join | iterable.join([separator]) | Joins elements into a string with separator | O(n) | essential |
abs / ceil / floor / round | num.abs() / num.ceil() / num.floor() / num.round() | Absolute value, ceiling, floor, or rounded value | O(1) | essential |
min / max (dart:math) | min(a, b) / max(a, b) | Returns minimum or maximum of two numbers | O(1) | essential |
insert | list.insert(index, element) | Inserts an element at the specified index | O(n) | common |
removeWhere | list.removeWhere((e) => condition) | Removes all elements matching a condition | O(n) | common |
expand | iterable.expand((e) => iterable) | Flattens nested iterables (flatMap equivalent) | O(n) | common |
every / any | iterable.every((e) => cond) / iterable.any((e) => cond) | Checks if all/any elements satisfy the condition | O(n) | common |
take / skip | iterable.take(n) / iterable.skip(n) | Takes first n elements or skips first n elements | O(1) lazy | common |
codeUnitAt / String.fromCharCode | str.codeUnitAt(index) / String.fromCharCode(code) | Gets UTF-16 code unit or creates string from code | O(1) | common |
forEach | iterable.forEach((e) => action) | Executes function for each element | O(n) | common |
asMap | list.asMap() | Returns map with indices as keys for index-based iteration | O(1) | common |