Search code examples
flutterfunctionparameters

Flutter - Parameter Type for a Method


I am working a FLUTTER project and need to write a method (or maybe a function). One of the parameters in the method needs to be an expression like:

PlayList videos = videoList[index] as PlayList

Is it possible? If so what type should I should use.


Solution

    • typedef can set the data type, method parameters
    typedef ProcessCallback = PlayList Function();
    
    
    • set as method parameter
    void foo(ProcessCallback callback) {
       /// result type is PlayList
       var result =  callback.call();
    }
    
    
    • when calling the foo method
     ProcessCallback callback = () => videoList[index] as PlayList;
     foo(callback);
    
    
    • Before actually running the callback.call() method, it is just a data type: Closure: () => PlayList
    • .call() can also be replaced with ();