Firestore / flutterFire / type 'List<dynamic>' 不是类型 'List<bool>' 的子类型

Posted

技术标签:

【中文标题】Firestore / flutterFire / type \'List<dynamic>\' 不是类型 \'List<bool>\' 的子类型【英文标题】:Firestore / flutterFire / type 'List<dynamic>' is not a subtype of type 'List<bool>'Firestore / flutterFire / type 'List<dynamic>' 不是类型 'List<bool>' 的子类型 【发布时间】:2021-08-25 22:43:47 【问题描述】:

我有以下 StoreModel 类;

class StoreModel 
  StoreModel(
      @required this.stripeCustomerId,
      @required this.dateCreated,
      @required this.storeSchedule,
      @required this.expired,
      @required this.userUid,
      @required this.images,
      @required this.thumbs,
      @required this.location,
      @required this.address,
      @required this.phone,
      @required this.garden,
      @required this.farm,
      @required this.craft,
      @required this.cook,
        @required this.description,
      );

  

  String stripeCustomerId;
  Timestamp dateCreated;
  Map<String, List<bool>> storeSchedule;  //< here
  bool expired = false;
  String userUid;
  List<String> images;
  List<String> thumbs;
  GeoPoint location;
  String address;
  String phone;
  bool garden;
  bool farm;
  bool craft;
  bool cook;
  String description;

  factory StoreModel.fromMap(Map<String, dynamic> data()) 
    if (data() == null) 
      return null;
    
    final String stripeCustomerId = data()['customer_id'];
    final Timestamp dateCreated = data()['dateCreated'];
    final Map<String, List<bool>> storeSchedule = Map.from(data()['storeSchedule']); // <-- and here

    final bool expired = data()['expired'];
    final String userUid = data()['userUid'];
    final List<String> images = List.from(data()['images']);
    final List<String> thumbs = List.from(data()['thumbs']);
    final GeoPoint location = data()['location'];
    final String address = data()['address'];
    final String phone = data()['phone'];
    final bool garden = data()['garden'];
    final bool farm = data()['farm'];
    final bool craft = data()['craft'];
    final bool cook = data()['cook'];
    final String description = data()['description'];

    return StoreModel(
      stripeCustomerId: stripeCustomerId,
      dateCreated: dateCreated,
      storeSchedule: storeSchedule,
      expired: expired,
      userUid: userUid,
      images: images,
      thumbs: thumbs,
      location: location,
      address: address,
      phone: phone,
      garden: garden,
      farm: farm,
      craft: craft,
      cook: cook,
      description: description,
    );
  

  Map<String, dynamic> toMap() 
    return 
      'customer_id': stripeCustomerId,
      'dateCreated': dateCreated,
      'storeSchedule': storeSchedule,
      'expired': expired,
      'userUid': userUid,
      'images': images,
      'thumbs': thumbs,
      'location': location,
      'address': address,
      'phone': phone,
      'garden': garden,
      'farm': farm,
      'craft': craft,
      'cook': cook,
      'description' : description,
    ;
  

我有一个由 Map> 组成的 customCalendar 设置,如下所示:

Map<String, List<bool>> calendarCompile = 
    '0': [false, false, false, false, false, false, false, false],
    '1': [false, false, false, false, false, false, false, false],
    '2': [false, false, false, false, false, false, false, false],
    '3': [false, false, false, false, false, false, false, false],
    '4': [false, false, false, false, false, false, false, false],
    '5': [false, false, false, false, false, false, false, false],
    '6': [false, false, false, false, false, false, false, false],
  ;

上传到 Firestore 可以完美运行,但从 Firestore 获取并使用 From.Map 工厂函数返回 > 未处理的异常:类型“列表”不是类型转换中“列表”类型的子类型。有什么想法吗?

【问题讨论】:

您的 storeSchedule 变量是 Map> 但您的 from.Map 方法返回 Map 转换可能像 ( Map>)Map .from(data()['storeSchedule']) 【参考方案1】:

在下面更改这一行:

    final Map<String, List<bool>> storeSchedule = Map.from(data()['storeSchedule']); 

到这里:

    final Map<String, List<bool>> storeSchedule = (data()['storeSchedule'] as Map).map((k, v) => MapEntry(k, (v as List).map((e) => e as bool).toList()));

这会将data()['storeSchedule'] 转换为Map,通过映射每个列表项并将每个MapEntry 映射到MapEntry&lt;String, List&lt;bool&gt;&gt; 的类型并转换为boolean

【讨论】:

那也没用,我最终将我的 storeSchedule 设置为 Map 一直...直到我找到合适的阅读方式。 当你说它不起作用时,出现了什么错误? 一模一样,未处理的异常:类型'List'不是类型'List'的子类型,由于某种原因,转换不起作用...... 我认为缺少一些东西。如果我有什么想法会告诉你的。 好的,我会试试的,没想到,回来找你

以上是关于Firestore / flutterFire / type 'List<dynamic>' 不是类型 'List<bool>' 的子类型的主要内容,如果未能解决你的问题,请参考以下文章

FlutterFire Firestore 2.0.0 与流式集合的迁移问题 [重复]

如何使用具有 Null 安全性的 FlutterFire Firestore 和 Flutter 2.0 从 Firebase 获取数据

FlutterFire DocumentSnapshot.data() 正在返回对象?

使用 FlutterFire 的 Firebase 自定义通知

使用 Flutterfire 堆栈获取 JSON 文件

FlutterFire 消息:您的后台消息处理程序中发生错误