Flutter FirebaseMessaging。发送通知时出错:类型“Null”不是类型转换中类型“Object”的子类型

Flutter FirebaseMessaging. Error sending notification: type 'Null' is not a subtype of type 'Object' in type cast

提问人:Yellow_Ananas 提问时间:11/15/2023 最后编辑:Munsif AliYellow_Ananas 更新时间:11/15/2023 访问量:47

问:


Future<void> sendNotificationToUser(String friendUid, String? title, String? body) async {
  try {
    DocumentSnapshot friendSnapshot = await FirebaseFirestore.instance.collection('users').doc(friendUid).get();
    String? friendFcmToken = await friendSnapshot['token_push'];
    print("friendFcmToken $friendFcmToken");

    await _firebaseMessaging.requestPermission();
    final String? fcmToken = await _firebaseMessaging.getToken();
    
    if (friendFcmToken != null) {
      await _firebaseMessaging.sendMessage(
        to: friendFcmToken,
        data: {
          'title': title!,
          'body': body!,
        },
      );
      print('Notification sent successfully');
    } else {
      print('Friend user does not have an FCM token');
    }
  } catch (e) {
    print('Error sending notification: $e');
  }
}

您好,我是第一次使用 FirebaseMessaging,遇到了一个问题。

enter image description here

请帮我弄清楚问题出在哪里。

我在聊天中发送通知。

Flutter 飞镖 推送通知 firebase-cloud-messaging

评论

0赞 Munsif Ali 11/15/2023
表单您收到此错误是哪一行?
0赞 Yellow_Ananas 11/15/2023
等待 _firebaseMessaging.sendMessage(
0赞 Munsif Ali 11/15/2023
通过打印检查,我认为您得到的是空值。fcmToken
0赞 Yellow_Ananas 11/15/2023
该值不为零。它被输出到控制台,并且可以显示在屏幕上。即使使用自己的令牌,也会出现相同的错误。

答:

0赞 Shahed Emon 11/15/2023 #1
 data: {
      'title': title ?? '',
      'body': body ?? '',
    },

评论

0赞 Yellow_Ananas 11/15/2023
不,它没有帮助
0赞 Shahed Emon 11/15/2023
String? friendFcmToken = friendSnapshot['token_push'] as String?;你能试试这个吗?
0赞 Yellow_Ananas 11/15/2023
问题仍然存在,也许我做错了什么?您能简要描述一下各个阶段吗?
0赞 Shahed Emon 11/15/2023
https://github.com/firebase/flutterfire/issues/10951 对不起