This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:collection/collection.dart'; | |
void main() { | |
// Mutable set | |
final mutableSet = {1, 2, 3, 3}; | |
mutableSet.forEach((e) => print(e)); // 1 2 3 | |
// Empty mutable set | |
final emptySet = <int>{}; | |
// Immutable set (copy of the original) | |
final immutableSet = Set.unmodifiable({1, 2, 3}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:collection/collection.dart'; | |
void main() { | |
// Mutable growable list | |
final mutableList = [1, 2, 3]; | |
mutableList.add(499); | |
final element = mutableList[3]; | |
mutableList.length = 2; // truncate | |
// Mutable fixed-length list | |
final fixedLengthList = List.filled(5, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Iterable<int> oddNumbers(int num) sync* { | |
for (int odd = 1; odd / 2 < num; odd += 2) { | |
yield odd; | |
} | |
} | |
void main() { | |
// Synchronous generation function (returns Iterable) | |
oddNumbers(3) | |
.map((num) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NumberExtensions on String { | |
int parseInt() { | |
return int.parse(this); | |
} | |
} | |
extension ListExtensions<T> on List<T> { | |
List<List<T>> split(int at) { | |
return [sublist(0, at), sublist(at)]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Vehicle { | |
String brand; // Public | |
int _year = 0; // Private | |
int get year => _year; // Getter | |
set year(int value) { // Setter | |
if (value < 0) { | |
throw ArgumentError(); | |
} | |
_year = value; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:isolate'; | |
void compute(int num) { | |
final isolateName = Isolate.current.debugName; | |
print('Isolate $isolateName computing $num...'); | |
} | |
main() async { | |
print('Spawning...'); | |
final isoFut1 = Isolate.spawn(compute, 1, debugName: "I1"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<String> loadImage(String name) async { | |
await Future.delayed(Duration(seconds: 1)); | |
return "IMAGE$name"; | |
} | |
displayImages(String image1, String image2) { | |
print("$image1 | $image2"); | |
} | |
void main() async { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<String> requestToken() async { | |
await Future.delayed(Duration(seconds: 1)); | |
return "TOKEN"; | |
} | |
Future<void> postUserName(String token, String name) async { | |
await Future.delayed(Duration(seconds: 1)); | |
} | |
void main() async { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
greet(String name) => print("Hello $name"); | |
greet2(String person1, [String? person2]) { | |
print("Hello $person1"); | |
if (person2 != null) print("Hello $person2"); | |
} | |
String getGreeting({String name = "Dart"}) { | |
return "Hello $name"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
for (var i = 5; i >= 0; i = i - 2) { | |
print(i); // 5 3 1 | |
} | |
for (var num in [1, 4, 5, 2]) { | |
print(num); // 1 4 5 2 | |
} | |
final set = {"A", "B", "C"}; | |
set.forEach((letter) { | |
print(letter); // A B C |