【发布时间】:2021-05-25 10:00:10
【问题描述】:
我正在使用 StateNotifier 和 Riverpod。 我有一个通知页面,其中包含通知列表。当通知到达时,我会触发通知刷新。但是,当我导航到通知页面时,它仍然使用旧的(缓存?)列表。
如何在屏幕外刷新页面?
前台消息
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
_localNotification.showLocalNotification(message);
ProviderContainer().read(notificationProvider).getNotification();
});
后台消息
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
final LocalNotification localNotification = LocalNotification();
await localNotification.init();
localNotification.showLocalNotification(message);
ProviderContainer().read(notificationProvider).getNotification();
}
通知提供者
Future<void> getNotification() async {
try {
state = const NotificationLoadingState();
_notificationList = await _notificationRepository.getNotification();
state = NotificationLoadedState(_notificationList); // ==> I get a new list here
} catch (e, s) {
state = const NotificationErrorState('error fetching notification');
}
}
用户界面
final state = watch(notificationProvider.state);
if (state is NotificationLoadingState) {
return _buildLoading();
} else if (state is NotificationLoadedState) {
return _buildLoaded(state.notificationList); // ==> I still get the old list here
} else if (state is NotificationErrorState) {
return _buildError(state.message);
}
编辑:
我设法通过使用navigatorKey.currentContext 解决了前台消息处理程序。
但是,我仍然没有解决后台消息处理程序。
我尝试将main.dart 更改为将UncontrolledProviderScope 与全局ProviderContainer 一起使用,然后从后台消息处理程序中调用它。还是不清爽。
【问题讨论】: