'Passing parameters in Dart from one anonymous function to another using the variable name
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
totalCostOfProductintellMeThePricetries to print the function itself without printing the value returned by the function. - When
tellMeThePriceis 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?
Solution 1:[1]
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}) to String tellMeThePrice({required double totalCostOfProduct})
because it'e returning double data type.
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 double totalCostOfProduct}) {
return "THE PRICE IS ${totalCostOfProduct}";
}
print(
tellMeThePrice(
totalCostOfProduct: totalCostOfProduct(
taxRate: taxRate,
costOfProduct: costOfProduct,
),
).toString()
); // THE PRICE IS 10.814175
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kishan Busa |
