【发布时间】:2021-08-18 08:55:09
【问题描述】:
我正在尝试开发聊天应用程序。但是,如果我想向聊天发送消息,我会收到错误消息。而且我不知道我应该在哪里修复我的错误。有人可以帮助我并告诉我我做错了什么并在某个地方纠正我吗?
P/s:我还是 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);
}
}
【问题讨论】:
-
兄弟,我认为您的群聊 ID 为空,因此请确保它不为空
-
如何让它不为空?对不起,我不知道如何解决它
-
正如@Muhammad Arbaz Zafar 所说,您的
groupChatId似乎是问题所在。您可以在拨打FirebaseFirestore.instance.collection().doc()时检查它的值吗? -
我试着照你说的做,消息被保存到数据库中,但 idfrom 和 idto 为空
-
你在哪里定义
peerId?
标签: flutter mobile google-cloud-firestore