Issue
I have a the following basic example, which does not seem to work properly:
pubspec.yaml:
firebase_messaging: ^10.0.0
FcmService.dart
StreamSubscription fcmListener;
void init() {
fcmListener = FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// do stuff
});
}
void dispose() {
print('SUBSCRIPTION canceled');
fcmListener.cancel()
}
App.dart
void init() {
fcmService.init();
// other inits()
}
void dispose() {
print('EVERYTHING disposed');
fcmService.dispose();
// other disposes()
}
Problem
After I log in into my app the init()
method of App.dart is called, and everything is set up properly. The FCM service works all fine. When I log-out of the app the dispose()
method of App.dart is called and the app redirects to Login.dart. The proper logs are EVERYTHING disposed
and SUBSCRIPTION canceled
.
However,if I log in again (without hot reloading the app) I get the following error message, regarding fcmListener = FirebaseMessaging.onMessage.listen()
Unhandled Exception: Bad state: Cannot add new events while doing an addStream
. Although, the FCMService still works as expected.
This only happens in the new firebase_messaging, which they rewrote a while ago. I used this same code with a previous version of firebase_messaging, and this exception did not occur.
Am I missing something here?
Solution
Try to call .asBroadcastStream()
after FirebaseMessaging.onMessage
:
void init() {
fcmListener = FirebaseMessaging
.onMessage
.asBroadcastStream()
.listen((RemoteMessage message) {
// do stuff
});
}
The same method should be called with FirebaseMessaging.onMessageOpenedApp
. I have found this solution in a GitHub issue.
Answered By - BbL
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.