android - 从 Flutter 中的函数名获取函数

标签 android ios kotlin dart flutter

对于跨平台(iOS、android)项目,我考虑使用 DartFlutter 中使用 UI 编程.

为了直接翻译当前的 Kotlin 代码,我想通过函数名称调用函数。更准确地说,我正在寻找如何编写函数 getFunctionByName 以便以下代码打印“The answer is 42.”?

void function42(String s) {
    print(s + " 42.");
}

void main() {
    int number = 42;
    String calculatedFunctionName = "function" + number.toString();
    String calculatedArgument = "The answer is";
    Function f = getFunctionByName(calculatedFunctionName);
    f(calculatedArgument);
}

我努力实现的目标 works in Kotlin似乎work in Dart ,但是当我在 Flutter 项目中尝试链接的 Dart 代码时,导入

import 'dart:mirrors';

不适合我。有一整个thread about reflection in Flutter ,我的解释是,在撰写本文时,反射在 Flutter 中不起作用。但也许我想做的具体事情有解决方法?

最佳答案

这在 Flutter 中是不可能的。 Flutter 禁用 dart:mirror 以缩小二进制文件。

相反,flutter(现在是 angulardart)使用代码生成作为替代方案。

Dart 带有一个非常强大的代码生成器工具:build

关于android - 从 Flutter 中的函数名获取函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114663/

相关文章:

android - 类内的合成绑定(bind)上的空对象引用错误

android - 将 RecyclerView 放在 NestedScrollView 中,为所有项目调用 onBindView

java - onBackPressed() 覆盖错误

android - 更新 sdk 和 google-api 后导入 lib google.android.gms "Could not find class"

ios - 如何在 iOS swift 中检索缓存文件?

java - Kotlin 对象表达式 : Comparator example

java - 无法弄清楚为什么我的应用程序请求 root...?

ios - Xcode 11.4 - 为什么我的应用程序大小从 8 MB 增加到 80 MB 仅支持 iOS 12.0

ios - 为什么创建非 Controller 文件时默认没有@interface?

java - Kotlin 文件到 kotlin 类