dart, how to get generic type of method parameter using reflection?

Issue

I have test application:

import 'dart:mirrors';

class A {
  void eventHandlerInt(List<int> x){}
  void eventHandlerBool(List<bool> x){}
} 

void testMirrors(aFunction){
  ClosureMirror mir = reflect(aFunction);
  var param = mir.function.parameters.first;
  //How to get the Type T of List<T> of the first param?
}

void main() {
  var a = new A();
  testMirrors(a.eventHandlerInt);
  testMirrors(a.eventHandlerBool);
}

I would like to be able to find out what the generic type is of the first parameter of the method passed into testMirrors, so in the example above it would be int then bool. Is this even possible? if I inspect param the type property is null.

Solution

List<TypeMirror> types = mir.function.parameters.first.type.typeArguments;
param.forEach((e) => print(e.simpleName));

prints

Symbol(“int”)
Symbol(“bool”)

Answered By – Günter Zöchbauer

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.