[英]got an error in flutter require an identifier while using the streambuilder
[英]Getting an error of timestamp while using Streambuilder in flutter
使用 stream 构建器时出现错误,并且
看起来是由于数据类型不匹配,我试着自己找出来,但没能找到。
并且不知道到底是哪个语句导致了这个错误......
错误是
type 'Timestamp' is not a subtype of type 'DateTime?'
这是我的代码消息 model class
class MessageModel {
String? messageid;
String? sender;
String? msg;
bool? isseen;
DateTime? createdon;
MessageModel(
{this.messageid, this.sender, this.createdon, this.isseen, this.msg});
MessageModel.fromMap(Map<String, dynamic> map) {
messageid = map['messageid'];
sender = map['sender'];
msg = map['msg'];
isseen = map['isseen'];
createdon = map['createdon'];
}
Map<String, dynamic> toMap() {
return {
'messageid': messageid,
'sender': sender,
'msg': msg,
'isseen': isseen,
'createdon': createdon,
};
}
}
这是我在其中获取记录的 Streambuilder 代码
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chatrooms').doc(widget.chatroommodel.chatroomid).collection('messages')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
print('i am active ');
if (snapshot.hasData) {
print('hasdata');
QuerySnapshot querysnapshot =
snapshot.data as QuerySnapshot;
if (querysnapshot.docs.length > 0) {
print('length greate than zero');
//
List<Map<String, dynamic>> maplist = [];
for (int x = 0; x < querysnapshot.docs.length; x++) {
Map<String, dynamic> messagemap = querysnapshot.docs[x]
.data() as Map<String, dynamic>;
maplist.add(messagemap);
}
print(maplist);
return Expanded(
child: ListView.builder(
itemCount: maplist.length,
itemBuilder: (context, index) {
MessageModel tempmsg =
MessageModel.fromMap(maplist[index]);
return ListTile(
onTap: () {
},
,
title: Text(
tempmsg.msg.toString(),
style: TextStyle(fontSize: 20),
),
subtitle: Text(tempmsg.createdon.toString()),
);
}),
);
} else {
return Text('Has Data but No user Found');
}
} else {
if (snapshot.hasError) {
return Text('error found');
} else {
return Text('empty..');
}
}
} else {
return CircularProgressIndicator();
}
})
这是 function,我正在将数据设置为 Firestone
void sendmessage(String msg) async {
MessageModel temp = MessageModel(
messageid: uuid.v1(),
msg: msg,
sender: widget.usermodel.userid,
isseen: false,
createdon: DateTime.now(),
);
//here special reason not giving await..wtch chatapp video no.8
// if i place await..it will not allow to go further until msg deliver to server
// note it that firebase store it to localdevice if net not available and deliver it on net availability
//make some practise
FirebaseFirestore.instance
.collection('chatrooms')
.doc(widget.chatroommodel.chatroomid).collection('messages').doc(temp.messageid)
.set(temp.toMap());
txtmsgcontroller.clear();
}
它由来自 stackoverflow 的一些旧问题解决..
我只是错过了在消息 class model 中转换为日期
这是答案
MessageModel.fromMap(Map<String, dynamic> map) {
print(map['createdon']);
messageid = map['messageid'];
sender = map['sender'];
msg = map['msg'];
isseen = map['isseen'];
createdon = map['createdon'].toDate();
//i corrected this line and solved
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.