Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: cannot assigna type with generics to a Type variable

If I try to assign a type with generics to a Type variable I get an error, but if I use a runtimeType variable everything works fine.

  Type t = List;
  //Type t1 = List<int>;  // ERROR !!!
  Type t2 = new List<int>().runtimeType;
  print('$t $t2');  //> List List<int>

Is it a bug or there is something I'm not getting?

like image 850
J F Avatar asked Nov 18 '22 19:11

J F


1 Answers

Your first line is surprising, but, yes, it works. Class literals have type "Type":

print((List).runtimeType);
--> TypeImpl

print(List is Type);
--> true

The problem with the rest of your snippet is exactly the issue you found on github:

https://github.com/dart-lang/sdk/issues/11923

-- that class literals cannot use generics. You'll have to get an instance and use runtimeType to get the type you want here.

like image 84
David Morgan Avatar answered Jun 14 '23 13:06

David Morgan