List、Set 以及 Map 字面量也可以是参数化的。定义参数化的 List 只需在中括号前添加;定义参数化的 Map 只需要在大括号前添加 <keyType, valueType>:
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' };
classFoo<TextendsObject> { // Any type provided to Foo for T must be non-nullable. }
泛型方法
1 2 3 4 5 6 7 8
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; }