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

Flutter自定义view_cart事件报错:items参数类型不符合要求

自定义Firebase Analytics view_cart事件断言错误排查
  • 检查复制的字段转换函数是否覆盖了所有类型
    官方_marshalItems函数会确保购物车项的所有字段都是string或num类型,如果你在自定义参数里加入了bool、List、Map这类非允许的类型,哪怕经过filterOutNulls过滤,也会触发断言。比如给items加了is_selected: true这样的bool字段,就会报错。要么去掉这类字段,要么在_marshalItems里把非允许类型转成string/num(比如把bool转成"true"/"false"字符串)。

  • 核对logEvent的参数结构要求
    官方logViewCart调用的是内部封装的_logEvent方法,而你直接调用公开的logEvent时,items参数必须是由纯string/num类型键值对组成的Map数组。举个例子:

    // 错误示例:包含bool类型参数
    final items = [{"item_id": "123", "is_new": true}];
    // 正确示例:所有参数均为string/num
    final items = [{"item_id": "123", "is_new": "true"}];
    
  • 确认filterOutNulls的实现是否和官方一致
    官方的filterOutNulls是递归处理的,会过滤掉嵌套Map和List里的null值。如果你的复制版本只处理了顶层Map,没处理items数组里的子Map,可能埋下隐患。官方实现大致如下:

    Map<String, Object?> filterOutNulls(Map<String, Object?> map) {
      final filtered = <String, Object?>{};
      map.forEach((key, value) {
        if (value != null) {
          if (value is Map) {
            filtered[key] = filterOutNulls(value.cast<String, Object?>());
          } else if (value is List) {
            filtered[key] = value.map((e) => e is Map ? filterOutNulls(e.cast<String, Object?>()) : e).toList();
          } else {
            filtered[key] = value;
          }
        }
      });
      return filtered;
    }
    
  • 检查参数键名是否符合Firebase规范
    Firebase Analytics不允许参数键名包含空格、特殊字符,也不能使用官方保留字段名。如果你的自定义参数键名有大写、特殊符号,或者用了保留字,可能触发隐含的校验错误,导致断言报错。确保所有键名都是小写、无特殊字符的字符串。

内容的提问来源于stack exchange,提问作者Zahid Tekbaş

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 05:07:12