gpt4 book ai didi

dart - 如何通过Dart中的Mirrors调用operator-array-function

转载 作者:行者123 更新时间:2023-12-03 03:44:14 24 4
gpt4 key购买 nike

这适用于Dart-VM:

final List<String> names = [ "Bob", "JOE" ];
final InstanceMirror instanceMirror = reflect(names);

final Symbol function = new Symbol("[]");

final InstanceMirror getField = instanceMirror.invoke(function,[ 1 ]);
context = getField.reflectee;
_logger.info("Value $context"); // Shows JOE

但是如果我将其编译为JS,它将失败。
错误消息:`NoSuchMethodError:未找到方法:'[]'接收器:'JSArray'参数的实例:[1]'

我也尝试了elementAt而不是[],但是 我得到了: Unsupported operation: Can't use 'elementAt:1' in reflection because it is not included in a @MirrorsUsed annotation.

最佳答案

这个类从上面产生了错误(第一个样本减少了一点……)

const MdlComponentModelAnnotation MdlComponentModel = const MdlComponentModelAnnotation();

@MdlComponentModel
class ModelTest {

final ObservableProperty<String> wifi = new ObservableProperty<String>("never");

@MdlComponentModel // useless - but lights made this problem...
final List<ObservableProperty<String>> lights = [ new ObservableProperty<String>(""), new ObservableProperty<String>("") ];

final ObservableProperty<int> intensity = new ObservableProperty<int>(90);
}

这就是@Günther向我指出正确方向之后的`@MirrorsUsed'样子:
@MirrorsUsed(metaTargets: const [ MdlComponentModelAnnotation ],
targets: const [ 'List' ],
symbols: const [ '[]' ])
import 'dart:mirrors';

都! “目标”(列表)和“符号”([])是必要的!

关于dart - 如何通过Dart中的Mirrors调用operator-array-function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com