The `Future.value` constructor in Dart creates a future that is completed with a specified value.
The syntax of Future.Future.value constructor is:
Future.value([FutureOr<T> value ])
This Future.value constructor of Future creates a future completed with value.
Parameter | Optional/Required | Description |
---|---|---|
value | optional | The value with which the future should be completed. Defaults to null if not provided. |
In this example,
void main() {
Future<int> future = Future.value(42);
future.then((value) {
print('Future completed with value: $value');
});
}
Future completed with value: 42
In this example,
void main() {
String message = 'Hello, Dart!';
Future<String> future = Future.value(message);
future.then((value) {
print(value);
});
}
Hello, Dart!
In this example,
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Future<List<int>> future = Future.value(numbers);
future.then((value) {
print(value);
});
}
[1, 2, 3, 4, 5]
In this Dart tutorial, we learned about Future.value constructor of Future: the syntax and few working examples with output and detailed explanation for each example.