You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OneSignal发送通知时遇类型错误:String is not a subtype of type List<String>的解决求助

Fix: type 'String' is not a subtype of type 'List' in OneSignal Notification Send

Let's break down what's causing this error and how to fix it quickly:

The Root Cause

Your sendNotification function expects a list of strings (List<String>) as its first parameter (tokenIdList), but when you call it in _sendMessage, you're passing a single string (userData["tokenId"]). Dart throws this type mismatch error because it can't automatically convert a single String to a List.

Quick Fix: Wrap the Single Token in a List

Change your function call from this:

sendNotification(userData["tokenId"], _enterMessage, "Testing a message");

To this:

sendNotification([userData["tokenId"]], _enterMessage, "Testing a message");

By wrapping userData["tokenId"] in square brackets [], you're creating a List<String> with one element—exactly what the sendNotification function and OneSignal's API expect for the include_player_ids field.

Optional: Add Safety Checks

To avoid potential null errors (in case tokenId is missing from your Firestore document), add a quick validation:

final tokenId = userData["tokenId"];
if (tokenId != null) {
  sendNotification([tokenId as String], _enterMessage, "Testing a message");
} else {
  print("Warning: No token ID found for this user!");
  // You could also show a snackbar or log this to monitoring tools
}

Why This Works

OneSignal's API requires include_player_ids to be an array (list) of player IDs, even if you're sending to just one device. By passing a list instead of a single string, you're aligning your code with the API's requirements, which will resolve the type error and let your notifications send successfully.

内容的提问来源于stack exchange,提问作者Pannam T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:27:52