firebase - 读取 Firestore 集合时出现错误“无法将参数类型‘对象’分配给参数类型‘字符串’”
问题描述
我试图从 firebaseFirestore 获取数据,但它在下面显示错误,这篇文章也没有帮助https://firebase.flutter.dev/docs/firestore/usage/#typing-collectionreference-and-documentreference。
代码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:greatr/models/ChatRoom.dart';
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future getChatRooms() async {
List<ChatRoom> rooms = [];
QuerySnapshot response = await firestore.collection('chat_rooms').get();
response.docs.forEach((element) {
rooms.add(ChatRoom.fromJson(element.data()!));
});
}
错误
The argument type 'Object' can't be assigned to the parameter type 'String'.
解决方案
您不共享ChatRoom
该类的代码,但我假设它与您在问题中引用Movie
的文档中的类的示例相似。
如果这个假设是正确的,那么以下应该可以解决问题:
final chatRoomsRef = FirebaseFirestore.instance.collection('chat_rooms').withConverter<ChatRoom>(
fromFirestore: (snapshot, _) => ChatRoom.fromJson(snapshot.data()!),
toFirestore: (chat_room, _) => chat_room.toJson(),
);
//...
Future getChatRooms() async {
List<QueryDocumentSnapshot<ChatRoom>> chat_rooms = await chatRoomsRef
.get()
.then((snapshot) => snapshot.docs);
//...
}
推荐阅读
- javascript - XMLHttpRequest 何时被释放?
- javascript - JavaScript 时间解析
- visual-studio-code - 单击时,实时服务器扩展不起作用
- c - GCC - 在没有链接描述文件的情况下获取自定义部分的大小
- javascript - 反应:状态的布尔标志更新不够快
- c# - 如何在 Unity 中访问子对象中的车轮对撞机
- python - 筛算法的时间复杂度
- python - pygame中的文本框
- r - 为什么在控制台中成功运行的代码在 Rmarkdown 块中返回“错误:不完整的表达式”?
- python - Django Rest Framework + psycopg2:InterfaceError:游标已关闭