From 61aee808d032d264367c6e8ec96b354a19617e65 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 7 Jan 2026 20:45:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A9=BA=E5=85=B3=E9=94=AE=E8=AF=8D?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=97=B6=E4=B8=8D=E8=B0=83=E7=94=A8=20API?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=94=99=E8=AF=AF=206017?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - searchFriendByKey 添加空关键词检查 - searchGroupByKey 添加空关键词检查 - searchMsgByKey 添加空关键词检查 - 当关键词为空时直接清理数据,不请求 SDK --- .../view_models/tui_search_view_model.dart | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/business_logic/view_models/tui_search_view_model.dart b/lib/business_logic/view_models/tui_search_view_model.dart index 0d18d53..5231d9c 100644 --- a/lib/business_logic/view_models/tui_search_view_model.dart +++ b/lib/business_logic/view_models/tui_search_view_model.dart @@ -24,12 +24,17 @@ import 'package:tencent_cloud_chat_uikit/data_services/message/message_services. import 'package:tencent_cloud_chat_uikit/data_services/conversation/conversation_services.dart'; import 'package:tencent_cloud_chat_uikit/data_services/group/group_services.dart'; -enum KeywordListMatchType { V2TIM_KEYWORD_LIST_MATCH_TYPE_OR, V2TIM_KEYWORD_LIST_MATCH_TYPE_AND } +enum KeywordListMatchType { + V2TIM_KEYWORD_LIST_MATCH_TYPE_OR, + V2TIM_KEYWORD_LIST_MATCH_TYPE_AND +} class TUISearchViewModel extends ChangeNotifier { - final FriendshipServices _friendshipServices = serviceLocator(); + final FriendshipServices _friendshipServices = + serviceLocator(); final MessageService _messageService = serviceLocator(); - final ConversationService _conversationService = serviceLocator(); + final ConversationService _conversationService = + serviceLocator(); final GroupServices _groupServices = serviceLocator(); List? friendList = []; @@ -46,7 +51,8 @@ class TUISearchViewModel extends ChangeNotifier { List conversationList = []; Future?> initConversationMsg() async { - final conversationResult = await _conversationService.getConversationList(nextSeq: "0", count: 500); + final conversationResult = await _conversationService.getConversationList( + nextSeq: "0", count: 500); final conversationListData = conversationResult?.conversationList; conversationList = conversationListData ?? []; notifyListeners(); @@ -62,20 +68,33 @@ class TUISearchViewModel extends ChangeNotifier { } void searchFriendByKey(String searchKey) async { - final searchResult = - await _friendshipServices.searchFriends(searchParam: V2TimFriendSearchParam(keywordList: [searchKey])); + // 空关键词不调用 API,直接清理数据 + if (searchKey.trim().isEmpty) { + friendList = []; + notifyListeners(); + return; + } + final searchResult = await _friendshipServices.searchFriends( + searchParam: V2TimFriendSearchParam(keywordList: [searchKey])); friendList = searchResult; notifyListeners(); } void searchGroupByKey(String searchKey) async { - final searchResult = - await _groupServices.searchGroups(searchParam: V2TimGroupSearchParam(keywordList: [searchKey])); + // 空关键词不调用 API,直接清理数据 + if (searchKey.trim().isEmpty) { + groupList = []; + notifyListeners(); + return; + } + final searchResult = await _groupServices.searchGroups( + searchParam: V2TimGroupSearchParam(keywordList: [searchKey])); groupList = searchResult.data ?? []; notifyListeners(); } - void getMsgForConversation(String keyword, String conversationId, int page) async { + void getMsgForConversation( + String keyword, String conversationId, int page) async { void clearData() { currentMsgListForConversation = []; totalMsgInConversationCount = 0; @@ -99,8 +118,9 @@ class TUISearchViewModel extends ChangeNotifier { type: KeywordListMatchType.V2TIM_KEYWORD_LIST_MATCH_TYPE_OR.index, )); if (searchResult.code == 0 && searchResult.data != null) { - final messageSearchResultItems = searchResult.data!.messageSearchResultItems! - .firstWhereOrNull((element) => element.conversationID == conversationId); + final messageSearchResultItems = + searchResult.data!.messageSearchResultItems!.firstWhereOrNull( + (element) => element.conversationID == conversationId); totalMsgInConversationCount = messageSearchResultItems?.messageCount ?? 0; currentMsgListForConversation = [ ...currentMsgListForConversation, @@ -111,6 +131,14 @@ class TUISearchViewModel extends ChangeNotifier { } void searchMsgByKey(String searchKey, bool isFirst) async { + // 空关键词不调用 API,直接清理数据 + if (searchKey.trim().isEmpty) { + msgPage = 0; + msgList = []; + totalMsgCount = 0; + notifyListeners(); + return; + } if (isFirst == true) { msgPage = 0; msgList = [];