2
class A {
final List<int> value;
A(this.value);
static make(List<int> value) => new A(value);
}
// more class defs here
class Z {
final List<String> value;
Z(this.value);
static make(List<String> value) => new A(value);
}
然後下面是不行的,因爲類型沒有任何方法
Map<int, Type> typeMap = {0: A, 25: Z};
new typeMap[0]([1, 2, 3])
但下面的工作。雖然,創建靜態生成方法需要大量額外的工作。
Map<int, Function> funcMap = {0: A.make, 25: Z.make};
funcMap[0]([1, 2, 3])
有沒有更好的方法來做到這一點?
https://pub.dartlang.org/packages/source_gen,https://pub.dartlang.org/packages/reflectable或者'dart:mirrors'如果它不適合瀏覽器(帶鏡子的dart2js會導致代碼膨脹爲生成的JS輸出文件)。 –