![](/img/trans.png)
[英]Right way of using optional named parameters in Dart class constructor
[英]Dart: How does Dart match the named parameters in a Constructor of a Class?
Dart如何在類的構造函數中匹配命名的參數?
示例(有效):
Class MyWidget {
final String a;
final String b;
MyWidget (
@required this.a,
@required this.b
)
@override // Yes, it's Flutter
Widget build(BuildContext context) {
return ....
}
}
/// Calling MyWidget
return MyWidget(
a: x,
b: y
)
這按預期工作。 但是在此設置中,由於調用中的“ a”與MyWidget中的“ this.a”相同,因此我不得不在MyWidget中將變量命名為“命名參數”。
我想要的是這樣的:
Class MyWidget {
final String aaa;
final String bbb;
MyWidget (
@required a // And assign that value to this.aaa,
@required b // And assign that value to this.bbb
)
}
如何將傳遞的命名參數“ a”的值分配給局部變量“ aaa”?
您必須權衡this.xxx
語法的簡單性,如下所示:
class MyWidget {
final String aaa;
final String bbb;
MyWidget({a, b})
: aaa = a,
bbb = b;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.