# About Future generic type

**URL:** <https://forums.kodeco.com/t/about-future-generic-type/184765>\
**Category:** Dart Apprentice: Beyond the Basics\
**Created:** [March 6, 2023, 9:12am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765 "2023-03-06T09:12:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellery](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ellery/32/110937_2.png) [@ellery](https://forums.kodeco.com/u/ellery)\
**Post date:** [March 6, 2023, 9:12am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/1 "2023-03-06T09:12:16Z")

</div>

I have a question about the Future generic type below  
The code in the book is:

```dart
print('Before the future');

final myFuture = Future<int>.delayed(
  Duration(seconds: 1),
  () => 42,
)
    .then(
      (value) => print('Value: $value'),
    )
    .catchError(
      (Object error) => print('Error: $error'),
    )
    .whenComplete(
      () => print('Future is complete'),
    );

print('After the future');

```

`myFuture` is an instance of `Future` class. The type is inferred as `Future<void>`. But the right hand side is written as `Future<int>.delayed(` … How can this be possible? Or how do I understand the code above?

---

<div class="post-metadata">

**Author:** ![suragch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/suragch/32/109020_2.png) [@suragch](https://forums.kodeco.com/u/suragch)\
**Post date:** [March 7, 2023, 7:53am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/2 "2023-03-07T07:53:11Z")

</div>

The `Future<int>.delayed` constructor returns a `Future<int>`. The `int` refers to the value returned by `() => 42`. Since Dart can infer this type you can use `Future.delayed` directly without specifying the type. It’s still inferred to be `Future<int>`.

The confusion perhaps comes from the methods that are called on that returned future. Let me rewrite the code like so:

```dart
final myFuture = Future<int>.delayed(
  Duration(seconds: 1),
  () => 42,
);
final thenFuture = myFuture.then(
  (value) => print('Value: $value'),
);
final catchErrorFuture = thenFuture.catchError(
  (Object error) => print('Error: $error'),
);
final whenCompleteFuture = catchErrorFuture.whenComplete(
  () => print('Future is complete'),
);

```

The `then` method returns a `Future<void>`. So do the `catchError` and `whenComplete` methods. That’s why you see the inferred `Future<void>` for the original `myFuture`. The `myFuture` in my refactored code above has the expected `Future<int>` type.

---

<div class="post-metadata">

**Author:** ![ellery](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ellery/32/110937_2.png) [@ellery](https://forums.kodeco.com/u/ellery)\
**Post date:** [March 7, 2023, 8:00am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/3 "2023-03-07T08:00:20Z")

</div>

Thanks @suragch , your re-written code clears my thought.

---

<div class="post-metadata">

**Author:** ![kpere](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kpere/32/109586_2.png) [@kpere](https://forums.kodeco.com/u/kpere)\
**Post date:** [August 4, 2023, 11:07am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/4 "2023-08-04T11:07:34Z")

</div>

Thank you @ellery for your question! We appreciate your active participation in the community. Keep up the great work, and don’t hesitate to share more of your questions and insights. Your contributions make our community stronger and more vibrant. Happy learning!

---

<div class="post-metadata">

**Author:** ![kpere](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kpere/32/109586_2.png) [@kpere](https://forums.kodeco.com/u/kpere)\
**Post date:** [August 4, 2023, 11:08am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/5 "2023-08-04T11:08:11Z")

</div>

Thank you, @suragch , for your prompt and helpful response to the question. Your expertise is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![kpere](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kpere/32/109586_2.png) [@kpere](https://forums.kodeco.com/u/kpere)\
**Post date:** [August 4, 2023, 11:08am UTC](https://forums.kodeco.com/t/about-future-generic-type/184765/6 "2023-08-04T11:08:19Z")

</div>

Closed on August 4, 2023, 11:08am UTC


