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

Flutter聊天应用开发出现String is not a subtype of bool错误求助

错误根因

调用messageBubble组件时传参的位置顺序和组件定义的构造函数参数顺序不匹配:

  • messageBubble构造函数要求的位置参数顺序为:message(String) → isMe(bool) → name(String) → email(String)
  • 实际传入的位置参数顺序为:displayName(String) → email(String) → text(String) → uid比较结果(bool)
    第二个位置参数组件要求接收bool类型的isMe,实际传入了String类型的邮箱值,因此触发「String is not a subtype of bool」类型不匹配报错。
修复方案

调整调用messageBubble时的位置参数顺序,和构造函数定义保持一致即可:

itemBuilder: (context, index) => messageBubble(
  chatDoc[index]['text'], // 对应构造函数第一个参数message
  chatDoc[index]['uid'] == currentUser, // 对应构造函数第二个参数isMe
  chatDoc[index]['displayName'], // 对应构造函数第三个参数name
  chatDoc[index]['email'], // 对应构造函数第四个参数email
  key: ValueKey<String>(chatDoc[index].id),
  time: chatDoc[index]['time'].toDate(),
),

补充说明:如果currentUser是Firebase Auth返回的用户对象而非uid字符串,需要修改为chatDoc[index]['uid'] == currentUser?.uid,避免对象和字符串直接比较得到错误的布尔结果。

内容的提问来源于stack exchange,提问作者Gray Anthrax

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 10:24:03