How Manage Multiple pages EventBus Instance and Listener
Closed this issue · 1 comments
pslxx commented
like:
provider: AuthProvider.eventBus
WidgetPage: LivePage there is a socket connect, listen authProvider.eventBus.on then reconnect socket and send auth token,
now. LivePage dispose and rebuild !
how to remove Listener in page? if not there have duplicate event listener
sorry My English is very poor, can you understand me~
AuthProvider (Global~ Only one)
Future initUserInfo() async {
return await Request.getMyUserInfo().then((res) async {
Map data = res['data'];
setUser(UserInfoEntity().fromJson(data));
print("fire user login");
eventBus.fire(UserLoginEvent());
}).catchError((error) {
clearUser();
throw error;
});
}
RoomProvider (multiple , can dispose and rebuild)
Provider.of<AuthProvider>(context, listen: false)
.eventBus
.on<UserLoginEvent>()
.listen((event) {
print("----------------------------user login");
disposeSocket();
initSocket();
});
Provider.of<AuthProvider>(context, listen: false)
.eventBus
.on<UserLogoutEvent>()
.listen((event) {
print("----------------------------user logout");
disposeSocket();
initSocket();
});
pslxx commented
yeah,I know ,
can define a StreamSubscription~~~~ and StreamSubscription.cancel