美好的一天,我正在尝试将命名参数 {num phone} 应用于以下示例:
main(){
showInfo("[email protected]", "Fatima");
}
String showInfo(String email, String name, {num phone}) {
print(email);
print(name);
print(phone);
return "$email : $name : $phone";
}
但我收到错误:
Error: The parameter 'phone' can't have a value of 'null' because of its type 'num', but the implicit
default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
Future<String> showInfo(String email, String name, {num phone}) async {
^^^^^
感谢您的帮助。
您将参数标记为 a num
,这意味着它不能是null
。但是,未使用null
的命名参数的默认值是,因此您不能有一个可选的命名参数,其默认值为 ,null
并且数据类型不接受null
。
一种选择是给它一个默认值而不是null
:
String showInfo(String email, String name, {num phone = 0})
另一种选择是使它成为一个命名但必需的参数,因此它永远不会获得默认值:
String showInfo(String email, String name, {required num phone})
另一种选择是实际上保持电话可选:
String showInfo(String email, String name, {num? phone})
一些额外的智慧:电话号码可以以重要的前导零开头,并且在保存时不应删除。您不能使用num
电话号码,您必须使用string
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句