我如何为颤动的聊天应用程序创建一个集合
How can i create a collection for a chat app for flutter
我正在为一个聊天应用系统的应用构建一个部件,但我有点迷路了。
post 向 firebase 请求代码
FirebaseFirestore.instance
.collection('messages')
.doc(user.uid)
.collection(widget.receiver)
.add({
'message': _textEditingController.text,
'messageType': "sender",
'receiver': widget.receiver,
'sender': user.uid,
'timestamp': DateTime.now()
.millisecondsSinceEpoch
.toString(),
});
获取消息
FirebaseFirestore.instance
.collection('messages')
.doc(user.uid)
.collection(widget.receiver)
.where('receiver', isEqualTo: widget.receiver)
.snapshots(),
消息已发送,但如果我想检索它以供一对一使用,它不会显示。
我做错了什么吗?
你做错的是.doc(user.id)
。您正在为每个用户创建单独的文档,其中包含单独的集合。因此,要从用户那里获得消息,您需要倾听每个可能想给您发消息的用户。不知道widget receiver是什么,是用户ID吗?
嗯,在整个应用程序中发送消息的最简单方法是:
发送中:
FirebaseFirestore.instance
.collection('messages')
.add({
'sender': user.uid,
'receiver': reciever.uid, // user ID you want to read message
'message': _textEditingController.text,
'messageType': "sender", // i dont know why you need this ?
'created': FieldValue.serverTimestamp(), // I dont know why you called it just timestamp i changed it on created and passed an function with serverTimestamp()
});
正在获取消息:
FirebaseFirestore.instance
.collection('messages')
.where('created' ???) // This need to be first if you want to use orderby() method. Question marks for you to solve what need to be there.
.where('receiver', isEqualTo: user.uid) // This line is important to get any data out of message collection if you won't have it, rules will kick your request.
.where('sender', isEqualTo: friend.uid) // If you want to get documents only from specific friend.
.orderBy('created', 'desc') // ordered from newest to latest message.
.limit(20) // take just 20 documents. Max limit for one request 100 doc.
.snapshots(),
您将需要再次请求发送给用户的消息,并在应用中合并对话。
每个人都写在一个合集中,所以每个人只需要听一个合集。
如果您使用 firestore 规则限制对集合中文档的正确访问,则这种方式是可行的。
规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /messages/{messageId} {
allow create: if request.auth.uid != null
allow update: if request.auth.uid == resource.data.sender
allow read: if request.auth.uid == resource.data.sender || request.auth.uid == resource.data.recaiver
}
}
}
如果你想有多个接收者,只需从接收者字段中创建一个数组并更改有关读取操作的规则。
我正在为一个聊天应用系统的应用构建一个部件,但我有点迷路了。
post 向 firebase 请求代码
FirebaseFirestore.instance
.collection('messages')
.doc(user.uid)
.collection(widget.receiver)
.add({
'message': _textEditingController.text,
'messageType': "sender",
'receiver': widget.receiver,
'sender': user.uid,
'timestamp': DateTime.now()
.millisecondsSinceEpoch
.toString(),
});
获取消息
FirebaseFirestore.instance
.collection('messages')
.doc(user.uid)
.collection(widget.receiver)
.where('receiver', isEqualTo: widget.receiver)
.snapshots(),
消息已发送,但如果我想检索它以供一对一使用,它不会显示。 我做错了什么吗?
你做错的是.doc(user.id)
。您正在为每个用户创建单独的文档,其中包含单独的集合。因此,要从用户那里获得消息,您需要倾听每个可能想给您发消息的用户。不知道widget receiver是什么,是用户ID吗?
嗯,在整个应用程序中发送消息的最简单方法是:
发送中:
FirebaseFirestore.instance
.collection('messages')
.add({
'sender': user.uid,
'receiver': reciever.uid, // user ID you want to read message
'message': _textEditingController.text,
'messageType': "sender", // i dont know why you need this ?
'created': FieldValue.serverTimestamp(), // I dont know why you called it just timestamp i changed it on created and passed an function with serverTimestamp()
});
正在获取消息:
FirebaseFirestore.instance
.collection('messages')
.where('created' ???) // This need to be first if you want to use orderby() method. Question marks for you to solve what need to be there.
.where('receiver', isEqualTo: user.uid) // This line is important to get any data out of message collection if you won't have it, rules will kick your request.
.where('sender', isEqualTo: friend.uid) // If you want to get documents only from specific friend.
.orderBy('created', 'desc') // ordered from newest to latest message.
.limit(20) // take just 20 documents. Max limit for one request 100 doc.
.snapshots(),
您将需要再次请求发送给用户的消息,并在应用中合并对话。
每个人都写在一个合集中,所以每个人只需要听一个合集。 如果您使用 firestore 规则限制对集合中文档的正确访问,则这种方式是可行的。
规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /messages/{messageId} {
allow create: if request.auth.uid != null
allow update: if request.auth.uid == resource.data.sender
allow read: if request.auth.uid == resource.data.sender || request.auth.uid == resource.data.recaiver
}
}
}
如果你想有多个接收者,只需从接收者字段中创建一个数组并更改有关读取操作的规则。