Member-only story

The top 10 advanced concepts in Dart

Adil Essannouni
3 min readAug 23, 2023

--

Photo by Andrew Krasilnikov on Unsplash
  1. 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:

--

--

Adil Essannouni
Adil Essannouni

Written by Adil Essannouni

I am Adil Essannouni passionate about sport and web development. I would like to share my modest experience with you

No responses yet