在Dart泛型中使用动态

问题描述 投票:1回答:1

在Dart语言中FutureFuture<dynamic>类型有什么区别吗?例如,在函数的返回类型中:

Future f() { return ... }

Future<dynamic> f() { return ... }
generics dynamic dart
1个回答
1
投票

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

此外,在通用功能的情况下,通常可以推断出类型。

© www.soinside.com 2019 - 2024. All rights reserved.