【问题标题】: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'])

标签: flutter dart google-cloud-firestore


【解决方案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'的子类型,由于某种原因,转换不起作用......
  • 我认为缺少一些东西。如果我有什么想法会告诉你的。
  • 好的,我会试试的,没想到,回来找你
猜你喜欢
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2021-08-29
  • 2021-01-22
  • 2021-12-29
  • 2023-01-08
  • 2020-12-10
相关资源
最近更新 更多