首页 > 解决方案 > Firestore 返回列表而不是列表(扑)

问题描述

我有一个使用字符串数组设置的 firestore 数据库。在我的代码中,我有这个:

  factory Item.fromMap(Map<String, dynamic> map) {
    return Item(
      name: map['name'],
      imageUrls: map['imageUrls'];

imageUrls 是一个字符串列表。我用谷歌搜索了如何处理列表,但我认为它应该能够像这样处理它?我收到此错误:

  Expected a value of type List<String>, but got one of type List<dynamic>

感谢您的任何输入

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


您必须将 map['imageUrls'] 转换为 List

   factory Item.fromMap(Map<String, dynamic> map) {
        return Item(
          name: map['name'],
          imageUrls: (map['imageUrls'] as List)?.map((item) => item as String)?.toList();

推荐阅读