Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Created November 13, 2015 21:23
Show Gist options
  • Save sebnozzi/5a884cff78779ed8a45d to your computer and use it in GitHub Desktop.
Save sebnozzi/5a884cff78779ed8a45d to your computer and use it in GitHub Desktop.
Dart Callback
class Event {
}
class SomeComponent {
// Q: How to express the type of _callback?
var _callback = (Event e) { /* dummy implementation */};
// Called to register a callback
void onEvent( void callback(Event e) ) {
print("Registering callback...");
_callback = callback;
}
// Called to fire the event
void fireEvent() {
print("Fire!");
Event e = new Event();
_callback(e);
}
}
void main() {
var c = new SomeComponent();
c.onEvent((Event e){
print("The event $e has been fired!");
});
// On some other part of the program...
c.fireEvent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment