首页 > 解决方案 > Flutter/Dart - 无法使用静态访问访问实例成员“displayNotification”

问题描述

firebaseMessagingBackgroundHandler在一个名为FCMConfig.

以前,以下代码以前有效。

Future<void> firebaseMessagingBackgroundHandler(
    RemoteMessage _notification) async {
  print('Handling a background message: ${_notification.data["title"]}');
  String fcmname = _notification.data["name"];
  String fcmtitle = _notification.data["title"];
  String fcmmessage = _notification.data["message"];
  String title = _notification.data["title_key"];
FCMConfig.displayNotification(title: fcmtitle, body:fcmname);
}

但是在对 2.1.2 最低 SDK 进行一些更新后,我开始收到此错误;

 Instance member 'displayNotification' can't be accessed using static access.

我怎样才能解决这个问题?

标签: flutterdartfirebase-cloud-messaginginstance

解决方案


根据文档,您应该这样做:

Future<void> firebaseMessagingBackgroundHandler(
RemoteMessage _notification) async {
   print('Handling a background message: ${_notification.data["title"]}');
   String fcmname = _notification.data["name"];
   String fcmtitle = _notification.data["title"];
   String fcmmessage = _notification.data["message"];
   String title = _notification.data["title_key"];
   FCMConfig().displayNotification(title: fcmtitle, body:fcmname); // <---
}

您忘记了括号 ()。


推荐阅读