为什么在这种情况下不能使用扩展运算符? [英] Why I cant use spread operator in this case?

查看:292
本文介绍了为什么在这种情况下不能使用扩展运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写扩展statelessWidget的类,其构建方法返回 Text()小部件。并且我需要将来自 Map< dynamic,dynamic> 中的构造函数的文本选项(对齐,阶梯等)传递给它。为什么我不能像这样使用 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)
}


推荐答案

您目前只能将价差运算符用于集合文字( [] 用于列表 {} 用于地图设置)。

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

来自 Bob Nystrom的文章介绍了Dart 2.3的新功能:

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


点差只能出现在可以正常处理
接收零个或多个值的地方。在没有彻底改革
语言的执行模型并将其转换为Icon的情况下(我发现
颇具吸引力,但可能不切实际……),没有太多
可以满足这一约束。基本上是收集文字
,也可能是位置参数列表。 (我写了 $ b的提案$ b后一个,但是它很复杂,所以我们不这样做,至少现在不是
。)

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天全站免登陆