我有一个通知服务,它是一个更改通知器。当服务收到通知时,它会通知所有侦听器。我想在收听者收到通知时显示一个对话框。所以我在我的构建方法中执行以下操作
Consumer<NotificationService>(
builder: (BuildContext context, NotificationService notificationNotifier, _) {
if (notificationNotifier.hasNotifications)
_showNotification(context, notificationNotifier.popNotification());
return Scaffold(
这是shownotification方法
Future<dynamic> _showNotification(BuildContext context, NotificationModel notification) async {
try {
print(notification.title);
await PlatformAlertDialog(
notification.title,
notification.body,
).show(context);
} on UserFriendlyException catch (error) {
await PlatformAlertDialog(
error.title,
error.message,
).show(context);
}
}
所以但这会引发错误,因为我想在构建时构建对话框Unhandled Exception: setState() or markNeedsBuild() called during build.
我确实喜欢使用更改通知提供程序。那么我怎样才能使这项工作呢?