
问 题
I would want to be able to do something like this with a Dart class constructor:
class Model {
// ... setting instance variables
Model(Map fields) {
fields.forEach((k,v) => this[k] = v);
}
}

Obviously, this doesn't work, because this doesn't have a []= method.
Is there a way to make it work or is it simply not "the dart way" of doing things? If it's not, could you show me what would be the right way to tackle this?
解决方案

Currently no. You will have to wait for reflection to arrive in Dart before something like this (hopefully) becomes possible. Until then your best bet is probably to do it in a constructor. Alternatively you could try to use something like JsonObject which allows you to directly initialize it from a Map (for an explanation on how this works, check this blog post).
本文地址:IT屋 是否有一种方法来动态调用方式或在Dart中的类中设定范例变量?
问 题

我想用Dart类构造函数做这种的事情:
class Model {
// ...设置实例变量
模型(映射字段){
fields.forEach((k,v)=>这个[k] = v);
}
}
显然动态方法调用 为什么不安全,这不工作,因为此没有 [] = 方法。

$ b b
有没有办法让它工作,或者它根本不是飞镖的方法做人情?
解决方案
目前没有。如果没有,您可告诉我什么是恰当的方式来缓解这个难题。你将不得不等待反射到达Dart在这种的东西(希望)成为或许。直到如今,你最好的打赌可能是在一个构造函数。或者,您可以尝试使用类似于 JsonObject 的方式,它允许您从地图直接初始化它为此工作原理的表明动态方法调用 为什么不安全,请查看此 blog post)。
本文地址:IT屋 是否有一种方法来动态调用方式或在Dart中的类中设定范例变量?
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-144082-1.html
求直播
南海岛屿12海里之内属于中国领海