颤振火碱 参数类型 'Object?' 不能分配给参数类型"地图<字符串,动态>



我得到这个错误The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'.。我已经尝试了所有建议的答案,但我仍然得到相同的错误。

这些是我尝试过的一些解决方案。显然应该是data()而不是data

class Record {
final String buyer_name;
// final int total_quantity;
final String seller_location;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {required this.reference})
: assert(map['buyer_name'] != null),
// assert(map()['total_quantity'] != null),
assert(map['seller_location'] != null),
buyer_name = map['buyer_name'],
// total_quantity = map()['total_quantity'],
seller_location = map['seller_location'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
@override
String toString() => "Record<$buyer_name:$seller_location>";
}

我还将()添加到map,但我仍然得到相同的错误。

QuerySnapshotDocumentSnapshot类型现在是泛化的,这意味着您需要明确您希望从中获得的数据类型。

理想情况下,您应该使用迁移到cloud_firestore 2的文档中所示的类型声明用于读取该数据的查询。这样,您就可以在您共享的代码中得到以下内容:

Record.fromSnapshot(DocumentSnapshot<Map<String, dynamic> snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);

但如前所述:你需要将类型添加到更多的代码中,而不仅仅是你在问题中分享的,所以一定要学习并遵循我链接的文档。


或者,您也可以将snapshot.data()硬转换为您期望的类型:

Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data() as Map<String, dynamic>, reference: snapshot.reference);

相关内容

最新更新