我想在收到Firebase的后台通知时更新UI。
我有一个后台消息处理程序`
@pragma('vm:entry-point')
Future<void> handleBackgroundNotification(RemoteMessage remoteMessage) async {
try {
final SendPort send = IsolateNameServer.lookupPortByName('port_firebase');
send.send(remoteMessage.notification.body);
} catch (e) {
log(e.toString());
throw e;
}
}
And I am registering my Receive port and background handler inside
initState`。
bool res = IsolateNameServer.registerPortWithName(
_port.sendPort, 'port_firebase');
log('status $res');
if (res) {
log('port created successfuly');
_port.listen((dynamic data) {
log('port ' + data.toString());
}, onError: (e) {
log('error is ${e.toString()}');
});
}
FirebaseMessaging.onBackgroundMessage(handleBackgroundNotification);
它成功地注册了端口,但每当我收到后台通知时。它给我错误
NoSuchMethodError: The method 'send' was called on null.
Receiver: null
Tried calling: send("Welocme to FCM")
有人能解决这个问题吗。
在行中添加等待
final SendPort send = await IsolateNameServer.lookupPortByName('port_firebase');