问题描述
如飞镖文章所述:
所以我认为这会起作用:
So I assumed that this would work:
myList..clear().addAll(otherList);
这给了我我无法在null
上调用.addAll
的错误.
which gave me the error that I can't call .addAll
on null
.
因此,显然.
在..
之前,因此.addAll
已在.clear()
的结果上调用.
So apparently .
precedes ..
so that .addAll
has been invoked on the result of .clear()
.
我现在认为我有两种写法:
I figure now that I have two possibilities to write this:
-
myList..clear()..addAll(otherList);
-
(myList..clear()).addAll(otherList);
(如果我想获得.addAll()
的结果.
myList..clear()..addAll(otherList);
(myList..clear()).addAll(otherList);
(If I wanted to get the result of.addAll()
.
这是正确的吗?如果是,为什么决定赋予.
优先权?似乎很违反直觉.是否要避免这样的语法:myList(..clear().useResultOfClear()).addAll(otherList);
?
Is this correct? If yes, why the decision to give .
precedence? It seems very counterintuitive. Is it to avoid syntax like this: myList(..clear().useResultOfClear()).addAll(otherList);
?
推荐答案
您可以阅读Gilad Bracha的文章: Dart中的方法级联.在其末尾,您将看到许多示例.
You can read the article from Gilad Bracha : Method Cascades in Dart. At its ends, you will see many examples.
另请参见有关拉瑟尼尔森的答案,运算符优先级:
基本上,a..b().c()
与(t){t.b().c(); return t;}(a)
这篇关于方法级联如何在飞镖中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!