Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何删除重复列表,然后获取最新ID?_List_Flutter_Dart_Duplicates_Chat - Fatal编程技术网

List 如何删除重复列表,然后获取最新ID?

List 如何删除重复列表,然后获取最新ID?,list,flutter,dart,duplicates,chat,List,Flutter,Dart,Duplicates,Chat,我有这个列表: 然后我需要通过msg_id通过不同的聊天室获取最后一条消息,如下所示: [ {msg_id: 3, from_id: 10, to_id: 20, text: 'Some text again'}, {msg_id: 5, from_id: 15, to_id: 5, text: 'Hello World'} ]; 我试过distinct()和set()但是我还是搞混了。我该怎么办?因为你想要的是“按聊天室id分组”,我们需要先从id和to id生成它。我假设你的列表已

我有这个
列表

然后我需要通过
msg_id
通过不同的聊天室获取最后一条消息,如下所示:

[
  {msg_id: 3, from_id: 10, to_id: 20, text: 'Some text again'},
  {msg_id: 5, from_id: 15, to_id: 5, text: 'Hello World'}
];
我试过
distinct()
set()
但是我还是搞混了。我该怎么办?

因为你想要的是“按聊天室id分组”,我们需要先从id和to id生成它。我假设你的列表已经按msg id排序。我通过在排序后加入from id和to id来生成聊天室id。我们之所以需要首先对它们进行排序,是因为从_id10到_id20与从_id20到_id10是同一个聊天室

  List<Map> data = [
    {"msg_id": 1, "from_id": 10, "to_id": 20, "text": 'Some text123'},
    {"msg_id": 2, "from_id": 20, "to_id": 10, "text": 'Some text321'},
    {"msg_id": 3, "from_id": 10, "to_id": 20, "text": 'Some text again'},
    {"msg_id": 4, "from_id": 5, "to_id": 15, "text": 'Hello World'},
    {"msg_id": 5, "from_id": 15, "to_id": 5, "text": 'Hello World'}
  ];
​
  Map<String, Map> perRoom = {};
​
  data.forEach((d) {
    // getting room id
    List<int> roomIdList = [d["from_id"], d["to_id"]];
    // need to be sorted since from_id 10, to_id 20 is the same room as from_id 20, to_id 10
    roomIdList.sort();
    String roomId = roomIdList.join('-');
    perRoom[roomId] = d;
  });
​
  // convert Map<String, Map> back into List<Map>
  List<Map> onlyLatest = perRoom.values.toList();
​
  print(onlyLatest);
列表数据=[
{“msg_id”:1,“from_id”:10,“to_id”:20,“text”:“Some text123”},
{“msg_id”:2,“from_id”:20,“to_id”:10,“text”:“Some text321”},
{“msg_id”:3,“from_id”:10,“to_id”:20,“text”:“再次出现一些文本”},
{“msg_id”:4,“from_id”:5,“to_id”:15,“text”:“Hello World”},
{“msg_id”:5,“from_id”:15,“to_id”:5,“text”:“Hello World”}
];
​
Map perRoom={};
​
数据。forEach((d){
//获取房间id
列出roomIdList=[d[“from_id”],d[“to_id”];
//需要排序,因为从id 10到id 20的房间与从id 20到id 10的房间相同
roomIdList.sort();
字符串roomId=roomIdList.join('-');
perRoom[roomId]=d;
});
​
//将地图转换回列表
List onlyLatest=perRoom.values.toList();
​
打印(仅限最新版本);


如果您的列表来自查询,我真的建议您在数据库中设置聊天室id,因为您只需使用GROUP BY之类的功能,就可以避免从数据库中获取大量数据。

您考虑在这里复制什么?@D'Kayd chat room BY from id和to idso我们获得了字符串类型的聊天室id?@husainazkas yess。我想再没有比这更合适的了。当然,生成房间标识的方式取决于您自己。只需确保两个方向(从10到20和从20到10)生成相同的房间标识即可
  List<Map> data = [
    {"msg_id": 1, "from_id": 10, "to_id": 20, "text": 'Some text123'},
    {"msg_id": 2, "from_id": 20, "to_id": 10, "text": 'Some text321'},
    {"msg_id": 3, "from_id": 10, "to_id": 20, "text": 'Some text again'},
    {"msg_id": 4, "from_id": 5, "to_id": 15, "text": 'Hello World'},
    {"msg_id": 5, "from_id": 15, "to_id": 5, "text": 'Hello World'}
  ];
​
  Map<String, Map> perRoom = {};
​
  data.forEach((d) {
    // getting room id
    List<int> roomIdList = [d["from_id"], d["to_id"]];
    // need to be sorted since from_id 10, to_id 20 is the same room as from_id 20, to_id 10
    roomIdList.sort();
    String roomId = roomIdList.join('-');
    perRoom[roomId] = d;
  });
​
  // convert Map<String, Map> back into List<Map>
  List<Map> onlyLatest = perRoom.values.toList();
​
  print(onlyLatest);