- Functions are first class objects in Dart and can be passed as parameters to other functions.
- We pass a function to a widget essentially saying, “invoke this function when something happens”.
- Callbacks using interfaces like Android (<Java 8) have too much boilerplate code for a simple callback.
Java Functions are first class objects in Dart and can be passed as parameters to other functions.callback:
button.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
// Do something here
}
}
);
(Notice that this is only the code for setting up a listener. Defining a button requires separate XML code.)
Dart equivalent:
FlatButton(
onPressed: () {
// Do something here
}
)
(Dart does both declaration as well as setting up the callback.) This becomes much cleaner and organised and helps us avoid unnecessary complication.