You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@vr19860507 If you believe there would be some problem from clear() returning void, the point of using the cascade operator (items..clear()) instead of normal member access (items.clear()) is that items..clear() evaluates to items and ignores the return value of clear().
@vr19860507 The lint is telling you that you can use the cascade operator instead, as I showed in #58050. The lint seems correct; what makes you think that it is a false positive?
void main() {
final List<int> items = [];
var g = 7;
items.clear();
items.addAll([g]);
print(items);
items..clear()..addAll([g]);
print(items);
}
This will print out two identical lines because the two ways of writing the code are equivalent. In both cases any exiting contents of the list are removed (the invocation of clear) and then a single item is added to the list (the invocation of addAll).
I suspect that what wasn't clear is that the cascade operator means to invoke both clear and addAll on the result of the expression before the first cascade operator, which in this case is items.
Activity
[-]cascade_invocations[/-][+]`cascade_invocations` false positives[/+]jamesderlin commentedon Aug 13, 2020
Why is this a false positive? What's wrong with:
?
vsevolod19860507 commentedon Aug 14, 2020
The problem is with void!
bwilkerson commentedon Aug 14, 2020
Could you be a little more explicit? I don't see any particular problem caused by
void
in this example.jamesderlin commentedon Aug 18, 2020
@vr19860507 If you believe there would be some problem from
clear()
returningvoid
, the point of using the cascade operator (items..clear()
) instead of normal member access (items.clear()
) is thatitems..clear()
evaluates toitems
and ignores the return value ofclear()
.vsevolod19860507 commentedon Aug 19, 2020
@jamesderlin @bwilkerson
Just enable "cascade_invocations". And just paste this code
into the editor.
And you will understand what I'm talking about!
jamesderlin commentedon Aug 19, 2020
@vr19860507 The lint is telling you that you can use the cascade operator instead, as I showed in #58050. The lint seems correct; what makes you think that it is a false positive?
bwilkerson commentedon Aug 19, 2020
Try running the following code:
This will print out two identical lines because the two ways of writing the code are equivalent. In both cases any exiting contents of the list are removed (the invocation of
clear
) and then a single item is added to the list (the invocation ofaddAll
).I suspect that what wasn't clear is that the cascade operator means to invoke both
clear
andaddAll
on the result of the expression before the first cascade operator, which in this case isitems
.vsevolod19860507 commentedon Aug 19, 2020
@bwilkerson @jamesderlin Checked it now, it works!
But I remember exactly that when I opened this issue it did not work... I don't know, maybe I was mistaken, or maybe they fixed it...
Thank you anyway!
bwilkerson commentedon Aug 19, 2020
You're welcome. I'm glad it's working for you.