[英]why routing is not working when tap on the notification in flutter? ( the project is based on GetX and flutter local push notification package )
Good day.再会。 I know that we use the onSelectNotification property to get the callback when the user tap on the notification in using the flutter local push notification package.我知道当用户在使用 flutter 本地推送通知包时点击通知时,我们使用 onSelectNotification 属性来获取回调。
Future<void> _initialize() async {
await _configureLocalTimeZone();
AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
onDidReceiveLocalNotification: _onDidReceiveLocalNotification);
InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: _onSelectNotification); // This is for the callback
}
This is the _onSelectsNotification function.这是 _onSelectsNotification 函数。
Future _onSelectNotification(String payload) async {
if (payload != null) {
print('notification payload: $payload');
}
BuildContext context;
await Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) {
return NotificationsView();
})); // ***** This is not working. with no error.
}
I am not sure what to use for the context.我不确定在上下文中使用什么。 I think the next route to the NotificationView() is not working because of the context value.我认为由于上下文值,到 NotificationView() 的下一个路由不起作用。 Could you please help me with this issue?你能帮我解决这个问题吗? I am now using the GetX for the state management.我现在使用 GetX 进行状态管理。
Thanks.谢谢。
I solved this problem with Get.toNamed() My fault was in not returning in the _onSelectNotification function.我用 Get.toNamed() 解决了这个问题我的错是在 _onSelectNotification 函数中没有返回。
Future _onSelectNotification(String payload) async {
if (payload != null) {
print('notification payload: $payload');
}
return Get.toNamed(Routes.NOTIFICATIONS);
}
This is working well.这运行良好。
But I still don't know how to get the context from another state class.但是我仍然不知道如何从另一个状态类中获取上下文。 In the previous post, I passed the null for the context.在上一篇文章中,我为上下文传递了 null。 I know that this was wrong.我知道这是错误的。 How to get?怎么获得?
The code is not working because by the time you call your Navigator, the widget is not finished building.A simple fix is to wrap the Navigator.of(context) with Future like this.代码不起作用,因为当您调用 Navigator 时,小部件还没有完成构建。一个简单的解决方法是像这样用 Future 包装 Navigator.of(context)。
Future.delayed(Duration.zero).then((_){
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context) {
return NotificationsView();
}
));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.