位置参数如何通过DART中的无效安全检查?
DART中的以下代码行不有效:
void introduce({String name, int age}) {
print('My name is $name and I am $age years old.');
}
编译器将抱怨参数的无效安全性。
但是,如果我们切换到位置参数,则编译器突然停止抱怨参数的无效安全性,并且以下是有效的:
void introduce(String name, int age) {
print('My name is $name and I am $age years old.');
}
The following lines of code aren't valid in Dart:
void introduce({String name, int age}) {
print('My name is $name and I am $age years old.');
}
The compiler will complain about the null safety of the parameters.
But if we switch to the positioned parameters, the compiler suddenly stops complaining about the null safety of the parameters, and the following becomes valid:
void introduce(String name, int age) {
print('My name is $name and I am $age years old.');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定位参数本质上是必需的。命名和位置可选参数由呼叫者选择传递,因此它们可能是
null
。可以通过标记必需的
来使其正常可选的命名参数。Positioned parameters are inherently required. Named and positional optional parameters are optionally passed by the caller and it's therefore possible that they're
null
. One can make the normally optional named parameters required by marking themrequired
.