How can I read the input parameter of a function that is passed into another function?

I am writing a method that accepts as its argument a function which also has one argument. Is it possible to access the argument of the passed-in function within the body of the method that receives the function?


Here’s the method’s syntax:
T theMethod(T Function(T inputT) theFunction) {}

The method (theMethod) is meant to return a T.

theMethod accepts as its argument a T function (theFunction) which takes another T as its argument (inputT).

I want to evaluate theFunction conditionally depending on some status of inputT.

I know I could achieve this if I pass in inputT as a separate parameter, but is there any way with the current syntax that I can access the value of inputT within the body of theMethod?

I’ve tried accessing inputT directly, as well as accessing it with dot-notation inside the function and neither of those seem to work.

>Solution :

inputT is not a variable, it’s just a part of the declaration of the type of theFunction. When you call theMethod, there is no way to pass an input to theFunction directly without creating another parameter.

Leave a Reply