Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign functions to class fields in dart?

I understand how to assign a function to a variable in dart but how about a class field? Im currently doing it like this:

class A{
    DivElement rootElement;
    void addClass(String newClass){
        rootElement.classes.add(newClass);
    }
}

but I was hoping dart would support doing it a little bit shorter, something like how you would with a regular variable:

class A{
    DivElement rootElement;
    addClass => rootElement.classes.add;
}

is there a syntax similar to the second code snippet in dart?

like image 264
Daniel Robinson Avatar asked Jul 28 '13 14:07

Daniel Robinson


Video Answer


1 Answers

You can either call the method or make a getter that returns the actual function:

class A {
  DivElement rootElement;
  get addClass => rootElement.classes.add;
}

or:

class A {
  DivElement rootElement;
  addClass(newClass) => rootElement.classes.add(newClass);
}
like image 99
Kai Sellgren Avatar answered Sep 21 '22 07:09

Kai Sellgren