[英]Flutter/Firestore - Error: A value of type 'List<String> Function(QuerySnapshot)' can't be assigned to a variable of type 'List<dynamic>'
I am trying to create a dynamic list view which displays document ids from Firebase. I have created the list of events from snapshot:我正在尝试创建一个显示来自 Firebase 的文档 ID 的动态列表视图。我已经从快照创建了事件列表:
List<String> _eventListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return doc.documentID == null ? doc.documentID : ' ';
}).toList();
}
But when I come to build the stream using this code:但是当我使用这段代码构建 stream 时:
Stream<List<String>> get events {
return DatabaseService().eventsCollection.snapshots().map((event) => _eventListFromSnapshot);
}
I get the following error:我收到以下错误:
Error: A value of type 'List<String> Function(QuerySnapshot)' can't be assigned to a variable of type 'List<String>'.
You forgot calling function _eventListFromSnapshot
.您忘记调用 function
_eventListFromSnapshot
。 Fix like this像这样修复
Stream<List<String>> get events {
return DatabaseService()
.eventsCollection
.snapshots()
.map((snapshot) => _eventListFromSnapshot(snapshot));
}
or simpler:或更简单:
Stream<List<String>> get events {
return DatabaseService()
.eventsCollection
.snapshots().map(_eventListFromSnapshot);
}
I think we have to do it explicitly.我认为我们必须明确地这样做。
Try something like this in your method.在你的方法中尝试这样的事情。
List<String> _eventListFromSnapshot(QuerySnapshot snapshot) {
List<String> docIDs = [];
for(int i = 0; i < snapshot.documents.length; i++){
String value = snapshot.documents[i].documentID ?? " ";
docIDs.add(value);
}
return docIDs;
}
or else, we might have an issue in this line,否则,我们可能会在这一行中遇到问题,
Stream<List<String>> get events {
return DatabaseService().eventsCollection.snapshots().map((event) => _eventListFromSnapshot(event));
}
We have to call the function, instead of assigning it maybe.我们必须调用 function,而不是分配它。
Hope this solves your issue.希望这能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.