我正在听FirebaseMessaging.onMessage
。print语句按预期工作,但我想添加RemoteMessage
的streamController
不工作。
FirebaseMessaging.onMessage.listen((message) {
_streamController.add(message);
print(message.notification?.body ?? 'empty body'); // working perfectly
});
这里是streamcontroller
final StreamController<RemoteMessage> _streamController = StreamController();
Stream<RemoteMessage> notificationStream() {
return _streamController.stream;
}
这是我听流的地方:
class _HomePageState extends State<HomePage> {
late Stream<RemoteMessage> _stream;
@override
void initState() {
_stream = NotificationService().notificationStream();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
if (snapshot.hasData) return Text(snapshot.data!.notification!.body!);
return const Text('empty snapshot');
},
),
);
}
}
1条答案
按热度按时间cnjp1d6j1#
问题出在我的单例实现上,我把它改成了