如何解决“未处理的异常:类型'_InternalLinkedHashMap <dynamic,dynamic>'不是类型转换中类型'List <DocumentSnapshot>'的子类型”?

时间:2020-02-11 08:51:27

标签: flutter google-cloud-firestore

我正在使用带有Firestore的Flutter,并且想要获取数据。

这是我的收藏。 Firestore document

我的代码是:

  @override
  Stream<List<Notification>> publicNotifications() {
    return publicNotificationCollection
        .document('cEBC37lg7MtrRNCVcGFw')
        .snapshots()
        .asyncMap((snapshot) {
      List<Notification> notificationList;
      final notifications =
          snapshot.data['notifications'] as List<DocumentSnapshot>;
      for (final notification in notifications) {
        notificationList.add(Notification.fromEntity(
            NotificationEntity.fromSnapshot(notification)));
      }
      return notificationList;
    });
  }

然后我得到了错误:

Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<DocumentSnapshot>' in type cast

如何解决此错误? 预先感谢!

1 个答案:

答案 0 :(得分:0)

错误本身来自select * from a full join b using (id) where a.id = 123 or b.id = 123; 运算符。根据{{​​3}}:

仅当您确定对象属于特定类型时,才使用as运算符将其转换为特定类型。

从屏幕快照as中猜测的是嵌套地图类型的文档字段,我认为不可能将其转换为notification。这就是为什么您会出错。

我没有整个项目的全貌,但也许您必须弄清楚如何在不强制转换为所需的List<DocumentSnapshot>对象的情况下进行更改。 我看到的另一个选择是,但我不知道您可以更改Firestore结构的程度,是将通知更改为子集合,而不是我认为您将能够查询类似于Notifiaction的对象。

我希望这会有所帮助!