颤振随机字符串卡住



我被卡在这一点上了。我花了2天的时间想要弄清楚我怎样才能做到这一点。我试图获得随机通知的用户,一切都工作得很好,但当我调用通知它显示相同的字符串一遍又一遍,它不再更新。代码应该如何看起来像被更新和显示其他字符串从列表中没有再次触摸按钮?

String? randomName;
final random = new Random();
randomName = names[random.nextInt(names.length)];
onPressed: () {
showToast();
NotificationService()
.showNotification(1, 'Hello', randomName!);
},
Future<void> showNotification(int id, String title, String body) async {
await flutterLocalNotificationsPlugin.periodicallyShow(
id,
title,
body,
RepeatInterval
.everyMinute, //schedule the notification to show after 2 seconds.
const NotificationDetails(
// Android details
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "ashwin",
importance: Importance.max,
priority: Priority.max),
// iOS details
iOS: DarwinNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
// Type of time interpretation
androidAllowWhileIdle:
true, // To show notification even when the app is closed
);
}

我一直在尝试使用setState,但它不工作…

定期通知使用重复的标题重复体。它意味着用相同的内容一遍又一遍地显示相同的通知。如果你想动态显示内容,你需要一个定期的通知,以随机的方式从你的列表中获取数据。

看看这个问题,不同的场景,但相同的概念:如何改变通知数据,如标题和正文,当使用flutter_local_notification插件?

您可以尝试在显示通知的相同范围内生成随机名称,如下所示:

onPressed: () {
String? randomName;
final random = new Random();
randomName = names[random.nextInt(names.length)];
showToast();
NotificationService()
.showNotification(1, 'Hello', randomName!);
}

最新更新