I try to code my own examples / scenarios as I work through the book to make sure I understand what I’m reading / learning and not just relying on the book’s boiler plate. I started the passing functions to functions section and wrote the following code:
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)));
However when I run this I get the following error:
Unhandled exception:
NoSuchMethodError: Closure call with mismatched arguments: function 'main.<anonymous closure>'
Receiver: Closure: ({required double costOfProduct, required double taxRate}) => double
Tried calling: main.<anonymous closure>(taxRate: 0.0825, totalCostOfProduct: 9.99)
Found: main.<anonymous closure>({required double costOfProduct, required double taxRate}) => double
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1 _objectNoSuchMethod (dart:core-patch/object_patch.dart:85:9)
#2 main (file:///home/dj/Repos/Dart-Apprentice/Chapter%205/lesson.dart:24:45)
#3 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
This doesn’t make sense to me because I am passing a defined function object (totalCostOfProduct) to the tellMeThePrice function which requires a function that calculates the price as a named parameter. So it got that, and THAT function got it’s required doubles becuase they are previously assigned in the main function. The IDE isn’t throwing a fit, why is the dart compiler? What don’t I understand?
I suspect but can’t confirm this is a closure / scope issue.