Comments (3)
暂时不支持自定义ID功能,如果您需要实现该功能,可以参考以下解决方案:
class ImChatDataManager extends ValueNotifier<void> {
/// 当前界面聊天数据内容
List<DataEntity> data = [];
/// 发送消息
sendMessage(MessageNode node) async {
if (loginUserInfo == null) {
Fluttertoast.showToast(msg: "消息发送失败,请重试!");
return;
}
// 添加到消息队列
String randomId = UuidUtil.getUuid();
this.data.add(
DataEntity(
id: randomId,
status: ImChatMessageStatus.Sending,
message: MessageEntity(
id: this.id,
sessionType: type,
self: true,
elemList: [node],
userInfo: loginUserInfo,
),
),
);
// 更新滚动条到底部
notifyListeners();
toBottom();
// 发送消息,在有结果后进行更新操作
await MessageUtil.sendMessage(
session: SessionEntity(
id: this.id,
type: this.type,
),
node: node,
dateSender: (dateMessage) => this.insertDataByRandomId(
randomId,
dateMessage,
notify: false,
),
).then((res) {
this.updateDataStatusByRandomId(randomId, ImChatMessageStatus.Success, message: res);
}).catchError((res) {
this.updateDataStatusByRandomId(randomId, ImChatMessageStatus.Error);
});
notifyListeners();
toBottom();
}
/// 根据随机ID 更新消息对象
updateDataStatusByRandomId(String randomId, ImChatMessageStatus status, {MessageEntity message}) {
// 因为更新的对象一般都是发送消息时添加进去的,所以一定程度上来说,倒序查找要快一点
for (int index = this.data.length - 1; index > 0; index--) {
DataEntity entity = this.data[index];
if (entity.id == randomId) {
this.data[index].status = status;
if (message != null) {
this.data[index].message = message;
}
break;
}
}
notifyListeners();
}
/// 更新消息对象
updateMessage(MessageEntity message) {
for (int i = 0; i < this.data.length; i++) {
if (message == this.data[i].message) {
this.data[i].message = message;
notifyListeners();
break;
}
}
}
/// 删除消息
removeMessage(MessageEntity message) {
int index = -1;
for (int i = 0; i < this.data.length; i++) {
if (message == this.data[i].message) {
index = i;
break;
}
}
if (index != -1) {
this.data.removeAt(index);
notifyListeners();
}
}
/// 添加单条数据,根据随机ID
insertDataByRandomId(String randomId, MessageEntity item, {notify: true}) {
for (int i = 0; i < this.data.length; i++) {
if (this.data[i].id == randomId) {
this.data.insert(
i,
DataEntity(
message: item,
status: getMessageStatus(item),
),
);
if (notify) {
notifyListeners();
}
break;
}
}
}
/// 添加单条数据
insertData(int index, MessageEntity item, {notify: true}) {
this.data.insert(
index,
DataEntity(
message: item,
status: getMessageStatus(item),
),
);
if (notify) {
notifyListeners();
}
}
}
使用方式例子为:
imChatDataManager.sendMessage(ImageMessageNode(
path: value.resource,
));
DataEntity实体为:
/// 数据实体
class DataEntity {
/// 消息ID(仅在一次会话中有效)
String id;
/// 消息对象
MessageEntity message;
/// 状态
ImChatMessageStatus status;
DataEntity({
this.id,
this.message,
this.status,
});
}
/// Im消息状态
enum ImChatMessageStatus {
/// 发送中
Sending,
/// 发送成功
Success,
/// 发送失败
Error,
}
以上代码为我在其他项目中的实践,如果您有更好的办法,欢迎提出!
from fluttertencentimplugin.
参考以上回答,简要描述:
关键核心代码:
sendMessage(MessageNode node) async {
if (loginUserInfo == null) {
Fluttertoast.showToast(msg: "消息发送失败,请重试!");
return;
}
// 添加到消息队列
String randomId = UuidUtil.getUuid();
this.data.add(
DataEntity(
id: randomId,
status: ImChatMessageStatus.Sending,
message: MessageEntity(
id: this.id,
sessionType: type,
self: true,
elemList: [node],
userInfo: loginUserInfo,
),
),
);
// 更新滚动条到底部
notifyListeners();
toBottom();
// 发送消息,在有结果后进行更新操作
await MessageUtil.sendMessage(
session: SessionEntity(
id: this.id,
type: this.type,
),
node: node,
dateSender: (dateMessage) => this.insertDataByRandomId(
randomId,
dateMessage,
notify: false,
),
).then((res) {
this.updateDataStatusByRandomId(randomId, ImChatMessageStatus.Success, message: res);
}).catchError((res) {
this.updateDataStatusByRandomId(randomId, ImChatMessageStatus.Error);
});
notifyListeners();
toBottom();
}
实现思路为:
- 在消息节点之上再封装一个自定义数据对象,并存储想要的属性
- 先将待发送数据添加到消息队列,此时状态为发送中
- 在发送成功或失败后更新消息队列,此时状态为对应的发送状态
from fluttertencentimplugin.
问题解决了,感谢。
from fluttertencentimplugin.
Related Issues (20)
- android release包闪退 HOT 8
- 请问下新版的群简介怎么拿到,之前的版本会话列表中可以取到群简介 HOT 1
- 大佬能不能顺便也支持一下web HOT 2
- android createGroup 返回的groupID有非法字符 " HOT 2
- ios setGroupInfo必须指定全部字段,否则其他字段会被清空(android没这个问题) HOT 4
- 似乎是跟flutter_sound插件的依赖版本起冲突,请问该如何解决? HOT 4
- ios编译报错 HOT 1
- 请问直接关闭App的时候有办法让IM执行退出登录操作吗? HOT 1
- 如何更新本地的IM用户的信息呢? HOT 5
- Swift里的JsonUtil无法解析特殊字符 HOT 6
- flutter2.5.3 ios运行模拟器报错 HOT 16
- 可以支持下 insertC2CMessageToLocalStorage 这个方法嘛? HOT 2
- 发送失败后,调用resendMessage接口,并不能改变消息的发送状态status(依旧为MessageStatusEnum.SendFail) HOT 1
- ios 调用sendMessage 时 真机闪退 HOT 11
- 消息发送是否支持CloudCustomData HOT 9
- 麻烦添加一下x64支持 HOT 1
- 安卓机在进行会话监听时闪退 HOT 23
- `TencentImPlugin.setSelfInfo` 不生效或失败, 导致该用户聊天时获取到的`faceUrl`为null 或 不更新是旧头像, 但是试了腾讯官方的demo, 没有该问题 HOT 6
- DeviceToken
- 通过ref引入编译失败 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluttertencentimplugin.