List , Set 和 Map 字面量也是可以参数化的。 参数化字面量和之前的字面量定义类似, 对于 List 或 Set 只需要在声明语句前加 前缀, 对于 Map 只需要在声明语句前加 <keyType, valueType> 前缀
dart
1 2 3 4 5 6 7
var names = <String>['Seth', 'Kathy', 'Lars']; var uniqueNames = <String>{'Seth', 'Kathy', 'Lars'}; var pages = <String, String>{ 'index.html': 'Homepage', 'robots.txt': 'Hints for web robots', 'humans.txt': 'We are people, not machines' };
使用泛型函数
函数范型 first () 泛型可以在如下地方使用参数 T :
函数的返回值类型 (T)
参数的类型 (List)
局部变量的类型 (T tmp)
dart
1 2 3 4 5 6
T first<T>(List<T> ts) { // Do some initial work or error checking, then... T tmp = ts[0]; // Do some additional checking or processing... return tmp; }