EventBus should accept any Dart object
Closed this issue · 1 comments
marcojakob commented
We should simplify the EventBus API so that it will accept any Dart object.
At the moment we need to create EventType
s for every type of event we want to fire. By allowing every Dart object to be an Event we would not need to define EventType
s and could get rid of the EventType
class alltogether.
An Example
Define an arbitrary class to be used as event:
class UserLoggedInEvent {
User user;
UserLoggedInEvent(this.user);
}
Register listeners:
eventBus.on(UserLoggedInEvent).listen((UserLoggedInEvent event) {
print(event.user);
});
Fire events:
User myUser = new User('Mickey');
eventBus.fire(new UserLoggedInEvent(myUser));
Advantages
- No need for
EventType
, just use normal dart classes. - Custom defined event classes can carry as many data fields as needed.
- No name collisions. Compiler ensures that every event class can only defined once.
- With an inheritance structure of event classes we could create a hierarchical event bus. This allows us to define a common superclass of some events which we could listen to.
marcojakob commented
Available in v0.3.0 (6878d3d)