我收到错误type 'List<dynamic>' is not a subtype of type 'String'
错误指向return ChatRoomTile(snapshot.data.documents[index].data['participants']);
有人可以告诉我这是怎么回事吗?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: header(context, strTitle: "Messages",),
body: StreamBuilder(
stream: Firestore.instance.collection('/UserChats').where('participants',arrayContains: currentUser.receiverName).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
return ChatRoomTile(snapshot.data.documents[index].data['participants']);
//Text(snapshot.data.documents[index].documentID);
},
);
}else{return Text("Loading/Error");}
},
),
//getChatRooms(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.search),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => SearchChat()));
}
),
);
}
}
答案 0 :(得分:0)
通过用流将产生的类型声明数据,尝试包括StreamBuilder期望的数据类型,如下所示:
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('/UserChats').where('participants',arrayContains: currentUser.receiverName).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(snapshot.hasData){
// rest
}else{return Text("Loading/Error");}
},
),
然后取决于您要在ChatroomTile中显示什么。就像Owczar所说的那样,ChatroomTile将String作为参数。
答案 1 :(得分:0)
在我看来,snapshot.data.documents[index].data['participants']
是List
,而ChatRoomTile
的构造方法只接受String
。如果我没错,这是一个只有一项的列表,请尝试获取列表的第一项:
ChatRoomTile((snapshot.data.documents[index].data['participants'] as List<String>).first);