简体   繁体   English

如何使用 Dart 反射收听方法调用?

[英]How to listen to method invokation using Dart reflection?

i read a bit about dart mirrors but I did not figure out how to listen to method invocation using dart reflection .我读了一些关于 dart 镜像的信息,但我不知道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. Flutter 中的按钮可以有一个onTap()方法,当用户点击该按钮时会调用该方法。

I would like to listen to that invokation of onTap() using dart reflection to fire my own code when that happens...我想听听onTap()的调用,使用 dart 反射在发生这种情况时触发我自己的代码......

how to realize that?如何意识到这一点?

EDIT:编辑:

I want to inject code to a function that has a specific annotation...how to do this?我想向具有特定注释的 function 注入代码......怎么做?

It is impossible - Flutter completely disables the reflection ( dart:mirror ).这是不可能的 - Flutter完全禁用反射( dart:mirror )。 So you need to find other ways.所以你需要寻找其他方法。

See this for the disabled reflection: https://github.com/flutter/flutter/issues/1150看到这个禁用反射: https://github.com/flutter/flutter/issues/1150

EDIT编辑

Given you updated question, you need a code generator .鉴于您更新的问题,您需要一个代码生成器 For example, the json_annotation package.例如,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).如果这没有帮助,您可能需要https://github.com/alibaba-flutter/aspectd 之类的东西(警告:该项目可能好也可能不好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM