1

The reason it took me forever to find out is that I don't know how it is called. But I hope if I describe the question here as thoroughly as possible, it will pop up in the search results anyways.

Possible other titles:

How can I pass a function without name as parameter to querySelector.onClick.listen?

How to use a function without a name in dart?

How to create a nameless function that you can use only exactly there where it is defined?


I know that the following works - also hosted on dartpad

void main(){ querySelector('#btn').onClick.listen((e)=>fnct()); }

void fnct(){   querySelector('#btn').text="hola"; print("test");}

This changes the text of the button to "hola" and prints "test". But what if I don't want to define a new function just for this because I like to keep the flow when reading the code and don't like to jump from function to function needlessly?

lucidbrot
  • 5,378
  • 3
  • 39
  • 68

1 Answers1

0

After an hour of searching, I found this

For my own code example, it would be like this dartpad link:

void main(){
querySelector('#btn').onClick.listen((e){
  querySelector('#btn').text="hello";
  print("no hablo espanol");
                                        });
}

So you can define a function on the flow by using

(param){command(); secondCommand(param);}

It is entirely possible that you can find this somewhere. But I did not with my search terms. So if any of you know what the correct search terms would have been, let me know :)

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
  • 1
    Maybe "lambda" or "inline function". This might be interesting to you as well http://stackoverflow.com/questions/15804101/dart-lambda-shortland-function-confusion. In Dart it's good practice to use short hand function syntax for one-line functions. – Günter Zöchbauer Jul 02 '15 at 16:17