The `Future.microtask` constructor in Dart creates a future containing the result of calling a computation asynchronously with `scheduleMicrotask`.
The syntax of Future.Future.microtask constructor is:
Future.microtask(FutureOr<T> computation())
This Future.microtask constructor of Future creates a future containing the result of calling computation asynchronously with scheduleMicrotask.
Parameter | Optional/Required | Description |
---|---|---|
computation | required | The computation to be executed asynchronously. |
In this example,
void main() {
Future.microtask(() {
print('Microtask executed.');
});
}
Microtask executed.
In this example,
void main() {
int num = 10;
Future.microtask(() {
print('Double of $num is ${num * 2}');
});
}
Double of 10 is 20
In this example,
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Future.microtask(() {
for (int number in numbers) {
print(number);
}
});
}
1 2 3 4 5
In this Dart tutorial, we learned about Future.microtask constructor of Future: the syntax and few working examples with output and detailed explanation for each example.