简体   繁体   中英

Pass function as parameter in Dart

Hello: I need pass a function (event handler) as parameter to other function site in other class:

classB
{
    ClassB(){}
/***/
    load (void onData(Event e))
    {
        ImageElement ie=new ImageElement();
        ie.onLoad.listen( onData );  
        ie.src='hello.png';
    } 

/***/

}

classA
{
    List<ClassB> lcb=new List();

    ClassA(){}
  /****/

    void handler(Event e) {
        /****/
    }

    myFunction() { 
       /***/
        for (var i=0; i < lcb.length; i++)
        {   
            ***
            lcb[i].load( handler );
            ***
        }
       /***/
    }
}

It seems right in principle, but does not work. The function that is passed is never executed. Anyone have any idea what is the correct way?

Looks good and should work especially after your recent update with listen . Why do you expect the ImageElement to fire. You have to assign a src and attach it to the DOM to make it load and fire onload .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM