我正在编写扩展 statelessWidget 的类,它的构建方法返回 Text()
小部件。我需要将来自 Map<dynamic, dynamic>
中的构造函数的文本选项(对齐、阶梯等)传递给它.为什么我不能使用 spread
像这样的运算符
final Map textOptions;
final String text;
build(BuildContext context) {
return Text(text, ...textOptions)
}
最佳答案
您目前只能对集合文字使用展开运算符([]
用于 List
,{}
用于 Map
或 Set
).
来自 Bob Nystrom's article介绍 Dart 2.3 的新特性:
A spread can only appear in a place that can gracefully handle receiving zero or more values. Without completely overhauling the language’s execution model and turning it into Icon (which I find strangely appealing, but probably not practical…), there aren’t too many places that fit that constraint. Basically collection literals and maybe positional argument lists. (I wrote a proposal for the latter, but it’s quite complex so we aren’t doing it, at least not right now.)
但是,该提案并未完全涵盖您想要的内容,即将 Map
扩展到可选的命名参数中。
关于dart - 扩展运算符可以用于提供函数参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56277415/