使用变量名称将DART中的参数从一个匿名函数传递到另一个匿名函数
I'm reposting the (very slightly modified) code of a reader question from
void main() {
double taxRate = .0825;
double costOfProduct = 9.99;
Function totalCostOfProduct =
({required double taxRate, required double costOfProduct}) {
return (costOfProduct * taxRate) + costOfProduct;
};
print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));
String tellMeThePrice({required Function totalCostOfProduct}) {
return "THE PRICE IS ${totalCostOfProduct}";
}
print(
tellMeThePrice(
totalCostOfProduct: totalCostOfProduct(
taxRate: taxRate,
totalCostOfProduct: costOfProduct,
),
),
);
}
在dartpad中运行此问题给出以下错误:
TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
Error: TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
我认为这里有一些事情发生:
proting
in
in
nellmetheprice
尝试打印功能本身而不打印值,而无需打印值功能。- 当
tellmetheprice
实际上调用时,代码在函数(double)而不是函数本身的结果中传递。 (因此,类型错误。)
为了解决这些问题,我尝试了以下修改:
String tellMeThePrice({required Function totalCostOfProduct}) {
final result = totalCostOfProduct.call();
return "THE PRICE IS $result";
}
print(
tellMeThePrice(
totalCostOfProduct: (taxRate, costOfProduct) => totalCostOfProduct(
taxRate: taxRate,
totalCostOfProduct: costOfProduct,
),
),
);
这仍然返回一个错误:
TypeError: totalCostOfProduct.call$0 is not a function
Error: TypeError: totalCostOfProduct.call$0 is not a function
这是读者的一个人为示例使用两个函数。但是,这也使我陷入困境。如何将参数传递到第二个功能?
I'm reposting the (very slightly modified) code of a reader question from here.
void main() {
double taxRate = .0825;
double costOfProduct = 9.99;
Function totalCostOfProduct =
({required double taxRate, required double costOfProduct}) {
return (costOfProduct * taxRate) + costOfProduct;
};
print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));
String tellMeThePrice({required Function totalCostOfProduct}) {
return "THE PRICE IS ${totalCostOfProduct}";
}
print(
tellMeThePrice(
totalCostOfProduct: totalCostOfProduct(
taxRate: taxRate,
totalCostOfProduct: costOfProduct,
),
),
);
}
Running this in DartPad gives the following error:
TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
Error: TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
There are a few things going on here I think:
- Printing
totalCostOfProduct
intellMeThePrice
tries to print the function itself without printing the value returned by the function. - When
tellMeThePrice
is actually called, the code passes in the result of the function (a double) rather than the function itself. (Hence, the type error.)
In an attempt to solve these problems I tried the following modifications:
String tellMeThePrice({required Function totalCostOfProduct}) {
final result = totalCostOfProduct.call();
return "THE PRICE IS $result";
}
print(
tellMeThePrice(
totalCostOfProduct: (taxRate, costOfProduct) => totalCostOfProduct(
taxRate: taxRate,
totalCostOfProduct: costOfProduct,
),
),
);
This still returns an error:
TypeError: totalCostOfProduct.call$0 is not a function
Error: TypeError: totalCostOfProduct.call$0 is not a function
This is a contrived example by the reader to use two functions. However, it also stumped me. How do I pass the parameters on to the second function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,Total Costoffroduct正在返回双数据类型,并且您将TotalCostofproduct作为Tellmetheprice函数中的函数。
因此,它将返回双数据类型而不是函数。
只需更改
字符串tellmetheprice({必需函数totalCostofproduct})
字符串tellmetheprice({必需double toul double costostofproduct})
>因为它返回双数据类型。
The problem is that totalCostOfProduct is returning double data type and you are taking totalCostOfProduct as a function in the tellMeThePrice function.
So it will return a double data type instead of a function.
just change
String tellMeThePrice({required Function totalCostOfProduct})
toString tellMeThePrice({required double totalCostOfProduct})
because it'e returning double data type.