gpt4 book ai didi

dart - 在Dart中进行方法级联时如何引用 “this”?

转载 作者:行者123 更新时间:2023-12-03 03:28:21 25 4
gpt4 key购买 nike

在Dart中进行方法级联时,我想引用“this”(方法所有者)。

// NG code, but what I want.
paragraph.append( getButton()
..text = "Button A"
..onClick.listen((e) {
print (this.text + " has been has clicked"); // <= Error. Fail to reference "button.text".
}));

我知道我可以将这样的代码分成多行来编写。

// OK code, I think this is verbose.
var button;
button = getButton()
..text = "Button A"
..onClick.listen((e) {
print (button.text + " has been clicked");
}));
paragraph.append( button);

无法引用级联的源对象使我在许多情况下无法编写较短的代码。有更好的方法进行方法级联吗?

最佳答案

您不能随意使用this

您可以使用简化第二个代码段:

var button = getButton();
paragraph.append(button
..text = "Button A"
..onClick.listen((e) {
print (button.text + " has been has clicked");
}));

关于dart - 在Dart中进行方法级联时如何引用 “this”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com