【问题标题】:Flutter firebase, List<dynamic> is not of type List<String>Flutter firebase, List<dynamic> 不是 List<String> 类型
【发布时间】:2020-10-03 00:44:18
【问题描述】:

在分配来自 firestore 的数据时

List&lt;String&gt; idList = snap.data['idList']

我得到了错误

List&lt;dynamic&gt; is not of type List&lt;String&gt;

firestore 中的 idList 看起来像这样

【问题讨论】:

  • 请提供数据样本。
  • 你能提供firestore中idList的截图吗?

标签: firebase flutter types


【解决方案1】:

我们需要使用List.castFrom

List<String> idList = List.castFrom(snap.data['idList'] as List ?? []);

as List ?? [] 不是强制性的,但可以防止数据未定义或为空时出错。

【讨论】:

  • 这对我有用,但是如果我想将字符串列表添加到 Firestore,我该怎么做
猜你喜欢
  • 1970-01-01
  • 2021-06-09
  • 2021-09-30
  • 2022-12-02
  • 2022-11-02
  • 2022-11-11
  • 2023-01-07
  • 2021-01-22
  • 1970-01-01
相关资源
最近更新 更多