考虑以下类(class)。
class Test {
final String foo;
final dynamic bar;
Test({this.foo, this.bar});
Test.barInt({this.foo, this.bar});
}
字段
bar
如何在命名的structt int
中指定为barInt
?
最佳答案
您可以在参数之前指定类型:
Test.barInt({this.foo, int this.bar});
请注意,在代码的更高版本中,可以将
bar
的值从其 setter 更改为任何类型。您可能需要针对此用例 checkout generics。
关于dart - 在命名构造函数中指定动态变量的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473465/