[英]Flutter FirebaseMessaging - StreamSubscription not handled properly
I have a the following basic example, which does not seem to work properly:我有以下基本示例,它似乎无法正常工作:
pubspec.yaml: pubspec.yaml:
firebase_messaging: ^10.0.0
FcmService.dart FcmService.dart
StreamSubscription fcmListener;
void init() {
fcmListener = FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// do stuff
});
}
void dispose() {
print('SUBSCRIPTION canceled');
fcmListener.cancel()
}
App.dart App.dart
void init() {
fcmService.init();
// other inits()
}
void dispose() {
print('EVERYTHING disposed');
fcmService.dispose();
// other disposes()
}
After I log in into my app the init()
method of App.dart is called, and everything is set up properly.在我登录到我的应用程序后,调用 App.dart 的
init()
方法,并且一切都设置正确。 The FCM service works all fine. FCM 服务一切正常。 When I log-out of the app the
dispose()
method of App.dart is called and the app redirects to Login.dart.当我注销应用程序时,调用 App.dart 的
dispose()
方法,应用程序重定向到 Login.dart。 The proper logs are EVERYTHING disposed
and SUBSCRIPTION canceled
.正确的日志是
EVERYTHING disposed
和SUBSCRIPTION canceled
。
However,if I log in again (without hot reloading the app) I get the following error message, regarding fcmListener = FirebaseMessaging.onMessage.listen()
但是,如果我再次登录(没有热重新加载应用程序)我收到以下错误消息,关于
fcmListener = FirebaseMessaging.onMessage.listen()
Unhandled Exception: Bad state: Cannot add new events while doing an addStream
. Unhandled Exception: Bad state: Cannot add new events while doing an addStream
。 Although, the FCMService still works as expected.虽然,FCMService 仍然按预期工作。
This only happens in the new firebase_messaging, which they rewrote a while ago.这只发生在他们不久前重写的新 firebase_messaging 中。 I used this same code with a previous version of firebase_messaging, and this exception did not occur.
我在以前版本的 firebase_messaging 中使用了相同的代码,并且没有发生此异常。
Am I missing something here?我在这里错过了什么吗?
Try to call .asBroadcastStream()
after FirebaseMessaging.onMessage
:尝试在
FirebaseMessaging.onMessage
之后调用.asBroadcastStream()
:
void init() {
fcmListener = FirebaseMessaging
.onMessage
.asBroadcastStream()
.listen((RemoteMessage message) {
// do stuff
});
}
The same method should be called with FirebaseMessaging.onMessageOpenedApp
.应该使用
FirebaseMessaging.onMessageOpenedApp
调用相同的方法。 I have found this solution in a GitHub issue .我在GitHub 问题中找到了这个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.