在Dart语言中Future
和Future<dynamic>
类型有什么区别吗?例如,在函数的返回类型中:
Future f() { return ... }
和
Future<dynamic> f() { return ... }
Future
本身就相当于Future<dynamic>
。然而,GenericClass
总是等同于GenericClass<dynamic>
并不总是正确的。来自Fixing Common Type Problems:
考虑以下具有有界类型参数的泛型类,该参数扩展
Iterable
:class C<T extends Iterable> { final T collection; C(this.collection); }
...
在Dart 2中,当一个泛型类被实例化而没有显式类型参数时,每个类型参数默认为其类型边界(在本例中为
Iterable
),如果明确给出,或者dynamic
。
此外,在通用功能的情况下,通常可以推断出类型。