Reputation: 69
I'm trying to develop chat application. But I am getting error if I want to send a message to a chat. And I dont know where should I fix my error. Can someone help me and tell me what I did wrong and correct me somewhere?
P/s: im still new for chat development in flutter
void initState() {
super.initState();
listScrollController.addListener(_scrollListener);
groupChatId = '';
readLocal();
}
readLocal() async {
prefs = await SharedPreferences.getInstance();
id = prefs.getString('id');
if (id != null && !id.isEmpty) {
if (id.hashCode <= peerId.hashCode) {
groupChatId = '$id-$peerId';
} else {
groupChatId = '$peerId-$id';
}
FirebaseFirestore.instance
.collection('User')
.doc(id)
.update({'chattingWith': peerId});
setState(() {});
}
void onSendMessage(String content, int type) {
if (content.trim() != '') {
textEditingController.clear();
var docRef = FirebaseFirestore.instance
.collection('Message')
.doc(groupChatId)
.collection(groupChatId)
.doc(DateTime.now().millisecondsSinceEpoch.toString());
FirebaseFirestore.instance.runTransaction((transaction) async {
transaction.set(docRef, {
'idFrom': id,
'idTo': peerId,
'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
'content': content,
'type': type
});
});
listScrollController.animateTo(0.0,
duration: Duration(milliseconds: 300), curve: Curves.easeOut);
} else {
Fluttertoast.showToast(
msg: 'Nothing to send',
backgroundColor: Colors.black,
textColor: Colors.white);
}
}
Upvotes: 0
Views: 3025
Reputation: 21
That is because you are passing an empty value to the database. I had a similar problem where I was passing the last message to the database, and I was initializing it with "". Try putting something like a space " " instead.
Upvotes: 2