fetchContactList method

  1. @override
Future<List<ContactInfo>> fetchContactList()
override

获取所有的好友联系人信息 优先从缓存获取,失败则从SDK数据库本地获取,最后从服务端获取 包含FriendInfo 调用之后通过onContactListComplete 异步返回数据

Implementation

@override
Future<List<ContactInfo>> fetchContactList() async {
  var friendResult = await NimCore.instance.friendService.getFriendList();
  cleanCache();
  blockList = (await NimCore.instance.userService.getBlockList()).data ?? [];
  if (friendResult.isSuccess && friendResult.data != null) {
    final friendList = friendResult.data!;

    //获取需要拉取userInfo的好友列表
    final lackUserInfoFriends =
        friendList.where((friend) => friend.userProfile == null).toList();
    final friendAccList =
        lackUserInfoFriends.map((e) => e.accountId).toList();
    List<NIMUserInfo> userList = [];
    for (var accIds in splitList(friendAccList)) {
      final users = await NimCore.instance.userService.getUserList(accIds);
      if (users.isSuccess && users.data != null) {
        userList += users.data!;
      }
    }

    //填充ContactInfo 列表
    List<ContactInfo> contactList = friendList.map((e) {
      final contact = ContactInfo(
          e.userProfile ??
              userList.firstWhereOrNull(
                  (element) => element.accountId == e.accountId) ??
              NIMUserInfo(accountId: e.accountId),
          friend: e,
          isInBlack: blockList.contains(e.accountId) == true);
      contactMap[e.accountId] = contact;
      return contact;
    }).toList();
    _onContactListComplete.add(contactList);
    return contactList;
  } else {
    return List.empty();
  }
}