展开运算符可以用于提供函数参数吗? [英] Can the spread operator be used to supply function arguments?

查看:24
本文介绍了展开运算符可以用于提供函数参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写扩展 statelessWidget 的类,它的构建方法返回 Text() 小部件.我需要向它传递来自 Map 中的构造函数的文本选项(对齐、阶梯等).为什么我不能像这样使用 spread 运算符

I'm writing class that extends statelessWidget, its build method returns Text() widget. and I need to pass to it text options (alignment, stile etc.) that came from constructor in Map<dynamic, dynamic>. Why I can't use spread operator like this

final Map textOptions;
final String text;

build(BuildContext context) {
    return Text(text, ...textOptions)
}

推荐答案

您目前只能将扩展运算符用于集合文字([] for List, >{} 用于 MapSet).

You currently can use the spread operator only for collection literals ([] for List, {} for Map or Set).

来自 Bob Nystrom 的文章,介绍 Dart2.3的新特性:

From Bob Nystrom's article introducing Dart 2.3's new features:

点差只能出现在可以优雅处理的地方接收零个或多个值.无需彻底检修语言的执行模型并将其转换为 Icon(我发现奇怪的吸引人,但可能不实用......),没有太多许多地方符合该限制.基本上是集合文字也许位置参数列表.(我写了一个提案后者,但它相当复杂,所以我们不做,至少不做现在.)

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 传播到可选的命名参数中.

That proposal doesn't quite cover what you want, which is to spread a Map into optional named arguments, however.

这篇关于展开运算符可以用于提供函数参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆