Created
November 13, 2015 21:23
-
-
Save sebnozzi/5a884cff78779ed8a45d to your computer and use it in GitHub Desktop.
Dart Callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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