Pavel
Pavel

Reputation: 5876

Using dynamic in Dart generics

Is there any difference between Future and Future<dynamic> types in Dart language? For example, in function's return type:

Future f() { return ... }

and

Future<dynamic> f() { return ... }

Upvotes: 1

Views: 1229

Answers (1)

jamesdlin
jamesdlin

Reputation: 89926

Future by itself is equivalent to Future<dynamic>. However, it's not always true that GenericClass is always equivalent to GenericClass<dynamic>. From Fixing Common Type Problems:

Consider the following generic class with a bounded type parameter that extends Iterable:

class C<T extends Iterable> {
  final T collection;
  C(this.collection);
}

...

In Dart 2, when a generic class is instantiated without explicit type arguments, each type parameter defaults to its type bound (Iterable in this example) if one is explicitly given, or dynamic otherwise.

Also, in the case of generic functions, the type often can be inferred.

Upvotes: 3

Related Questions