How to listen to method invokation using Dart reflection?

545 Views Asked by At

i read a bit about dart mirrors but I did not figure out how to listen to method invocation using dart reflection.

Use Case:

Buttons in Flutter can have an onTap() method which is invoked when an user taps on that button.

I would like to listen to that invokation of onTap() using dart reflection to fire my own code when that happens...

how to realize that?

EDIT:

I want to inject code to a function that has a specific annotation...how to do this?

1

There are 1 best solutions below

6
ch271828n On

It is impossible - Flutter completely disables the reflection (dart:mirror). So you need to find other ways.

See this for the disabled reflection: https://github.com/flutter/flutter/issues/1150

EDIT

Given you updated question, you need a code generator. For example, the json_annotation package.

If that does not help, you may need things like https://github.com/alibaba-flutter/aspectd (Warn: that project may or may not be good).