Unable to retrieve typeArguments for reflected generic in Dart

41 Views Asked by At

I'm trying to get real types passed into generic, but I always get empty collection.

So far, to me it is mandatory to use dart:mirrors.

In future though I'm planning to migrate to reflectable.

Please hint me, where I'm wrong?

import 'dart:mirrors'

class A<T> {}

void main() {
  print(
    reflectClass(A<int>).typeArguments
  );
}

Output:

[]

Exited.
1

There are 1 best solutions below

0
Dan R On

You could try to use the function reflectType.

import 'dart:mirrors';

class A<T> {}

void main() {

  print(reflectType(A<int>).typeArguments);
}

Running this program in a terminal results in the following output:

$ dart main.dart
[ClassMirror on 'int']