Member-only story
The top 10 advanced concepts in Dart
3 min readAug 23, 2023
- Asynchronous Programming with `async` and `await`:
Dart supports asynchronous programming through the `async` and `await` keywords. This is used for dealing with tasks that might take some time, like network requests.
Future<void> fetchData() async {
print("Fetching data…");
await Future.delayed(Duration(seconds: 2));
print("Data fetched!");
}
2. Streams and Reactive Programming:
Streams are a powerful concept in Dart that allow you to work with asynchronous data sequences. This is particularly useful for scenarios like event handling.
Stream<int> countStream() async* {
for (int i = 1; i <= 5; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
void main() {
final stream = countStream();
stream.listen((data) {
print(data);
});
}
3.Generators:
Dart supports both synchronous and asynchronous generators, which can be used to produce a sequence of values lazily.
Iterable<int> countSync(int n) sync* {
for (int i = 1; i <= n; i++) {
yield i;
}
}
Stream<int> countAsync(int n) async* {
for (int i = 1; i <= n; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
4.Mixins: