简体   繁体   English

参数类型 'Stream<dynamic> ?' 不能分配给参数类型 'Stream<querysnapshot> ?'</querysnapshot></dynamic>

[英]The argument type 'Stream<dynamic>?' can't be assigned to the parameter type 'Stream<QuerySnapshot>?'

I am trying to use StreamBuilder to show a stream of text messages from an firestore database.我正在尝试使用 StreamBuilder 显示来自 firestore 数据库的 stream 文本消息。 But for some reason the argument type of Stream can't be assigned to the parameter of Querysnapshot .但是由于某种原因,无法将 Stream 的参数类型分配给Querysnapshot的参数。 If I get rid of the Querysnapshot the stream builder doesn't show the error anymore but then I get an error:如果我摆脱Querysnapshot stream 构建器不再显示错误,但随后我收到错误:

The getter 'documents' isn't defined for the type 'Object'没有为“对象”类型定义吸气剂“文档”

Can anyone help me out with this problem?谁能帮我解决这个问题?

Widget ChatMessageList() {
return StreamBuilder(
  stream: chatMessagesStream,
  builder: (context, snapshot) {
    return ListView.builder(
      itemCount: snapshot.data!.documents.length,
      itemBuilder: (context, index) {
        return MessageTile(snapshot.data!.documents[index].data['message']);
      },
    );
  },
);

} }

This kind of message appears because dart does not know what kind of object snapshot.data is.出现这种信息是因为dart不知道object snapshot.data是什么样的。 You probably need to specify the expected type that will be returned from your stream. What does chatMessagesStream return?您可能需要指定将从 stream 返回的预期类型。chatMessagesStream 返回什么? If it is Stream<Bar> , then write (context, AsyncSnapshot<Bar> snapshot) , like in the official example StreamBuilder如果是Stream<Bar> ,那么写(context, AsyncSnapshot<Bar> snapshot) ,就像官方例子中的StreamBuilder

Also, you should not do calls directly to firebase from your widgets.此外,您不应从小部件直接调用 firebase。 That is going to create lots of copy-paste and messy code.这将创建大量复制粘贴和混乱的代码。 You can find an example of a cleaner architecture here: real-time-updates-with-streambuilder您可以在这里找到一个更简洁的架构示例: real-time-updates-with-streambuilder

It will be a little extra work to set it up, but it will definitely pay off quickly ass you include more calls in your code.设置它会有点额外的工作,但它肯定会很快得到回报,因为您在代码中包含更多调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:参数类型'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>> Flutter 错误:参数类型 'QuerySnapshot<object?> ' 不能分配给参数类型 'Recipe'</object?> - Flutter error: The argument type 'QuerySnapshot<Object?>' can't be assigned to the parameter type 'Recipe' 参数类型 'Future<dynamic> ' 不能分配给参数类型 'String'</dynamic> - The argument type 'Future<dynamic>' can't be assigned to the parameter type 'String' '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?>' 元素类型 'Stream<dynamic> ' 不能分配给列表类型 'Stream <query snapshot<object?> >'</query></dynamic> - The element type 'Stream<dynamic>' can't be assigned to the list type 'Stream<Query Snapshot<Object?>>' 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>' 错误:参数类型 'Map<string, dynamic> ?' 不能分配给参数类型 'Map<dynamic, dynamic> '</dynamic,></string,> - Error: The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<dynamic, dynamic>' 参数类型“对象?” 不能分配给参数类型 'Map<dynamic, dynamic> '</dynamic,> - The argument type 'Object?' can't be assigned to the parameter type 'Map<dynamic, dynamic>' 参数类型“列表<dynamic> ' 不能分配给参数类型 'string?'。 在文档 snapShot flutter 中</dynamic> - the argument type 'list<dynamic>' can't be assigned to the parameter type 'string?'. In document snapShot flutter Flutter Cloud Firestore:错误:参数类型“对象? Function()' 无法分配给参数类型 'Map<string, dynamic> '</string,> - Flutter Cloud Firestore: error: The argument type 'Object? Function()' can't be assigned to the parameter type 'Map<String, dynamic>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM