Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Using IS Operator to check Generic Type

Tags:

generics

dart

I having some problem in checking the type of my variable when it is generic.

Example T is List<MyClass>

T is List<MyClass>
//return false
T is List
//return false

In the end, I have to use some stupid method in order to get the correct answer

T.toString() == "List<MyClass>"
//return true

Is there any standard way to deal with it, or I need to stick with my stupid method until official release?

like image 387
Geoffrey Lee Avatar asked Mar 04 '23 03:03

Geoffrey Lee


1 Answers

I made the "T is SomeClass" mistake before. T is a class so the "is" won't work on it.

On elements you should use T == MyClass.

On lists you should instantiate a List e.g. List<MyClass>() is T.

This is not a clean solution but List<MyClass> == T and List<MyClass>() == T won't work. Unfortunatly until now I did not figure out any better solution. Hope it works.

like image 157
MrGyovka Avatar answered May 24 '23 19:05

MrGyovka