简体   繁体   English

Flutter/Firestore - 错误:“列表”类型的值<string> Function(QuerySnapshot)' 不能分配给 'List' 类型的变量<dynamic> '</dynamic></string>

[英]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.

相关问题 'Future 类型的值<querysnapshot<map<string, dynamic> &gt;&gt;' 不能分配给“QuerySnapshot”类型的变量<object?> ' </object?></querysnapshot<map<string,> - A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>' Flutter 参数类型 &#39;StreamTransformer<dynamic, dynamic> &#39; 不能分配给参数类型 StreamTransformer <QuerySnapshot List<Message> &gt; - Flutter The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type StreamTransformer<QuerySnapshot List<Message>> Flutter 错误:参数类型'Stream <querysnapshot<map<string, dynamic> &gt;&gt;' 不能分配给参数类型'Future <querysnapshot<object?> &gt; </querysnapshot<object?></querysnapshot<map<string,> - Flutter error: The argument type 'Stream<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to the parameter type 'Future<QuerySnapshot<Object?>> 错误:参数类型'StreamTransformer<dynamic, dynamic> ' 不能分配给参数类型 'StreamTransformer <querysnapshot*, list<todo> &gt;</querysnapshot*,></dynamic,> - error: The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type 'StreamTransformer<QuerySnapshot*, List<Todo>> Firestore 参数类型 &#39;AsyncSnapshot<QuerySnapshot> &#39; 不能分配给参数类型 &#39;Map<String, dynamic> &#39; - Firestore The argument type 'AsyncSnapshot<QuerySnapshot>' can't be assigned to the parameter type 'Map<String, dynamic>' Flutter Firestore Stream builder "A value of type 'Stream <documentsnapshot<map<string, dynamic> &gt;&gt;' 不能分配给变量" </documentsnapshot<map<string,> - Flutter Firestore Stream builder "A value of type 'Stream<DocumentSnapshot<Map<String, dynamic>>>' can't be assigned to a variable" Flutter - 'List 类型的值<map<string, object> &gt;' 不能分配给“列表”类型的变量<classes> ' </classes></map<string,> - Flutter - A value of type 'List<Map<String, Object>>' can't be assigned to a variable of type 'List<Classes>' &#39;List 类型的值<Map<String, dynamic> &gt;&#39; 不能分配给“列表”类型的变量<ProjectBoxes> &#39; - A value of type 'List<Map<String, dynamic>>' can't be assigned to a variable of type 'List<ProjectBoxes>' &#39;List 类型的值<dynamic> &#39; 不能分配给 &#39;List 类型的变量<String> &#39; - A value of type 'List<dynamic>' can't be assigned to a variable of type 'List<String>' 将 JSON 数据转换为列表视图会导致类型为“List”的值的错误<XXX> &#39; 不能分配给类型为 &#39;Map 的变量<String, dynamic> &#39; - Converting JSON data to a list view results in an error of A value of type 'List<XXX>' can't be assigned to a variable of type 'Map<String, dynamic>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM