fetchContactList method
获取所有的好友联系人信息 优先从缓存获取,失败则从SDK数据库本地获取,最后从服务端获取 包含FriendInfo
Implementation
@override
Future<List<ContactInfo>> fetchContactList() async {
var friendResult = await NimCore.instance.userService.getFriendList();
var blackList = (await NimCore.instance.userService.getBlackList()).data;
if (friendResult.isSuccess && friendResult.data != null) {
final friendList = friendResult.data!;
//Android平台需要通过getUserInfoListAndroid批量获取用户信息
if (Platform.isAndroid) {
final friendAccList = friendList.map((e) => e.userId!).toList();
final userList = (await NimCore.instance.userService
.getUserInfoListAndroid(friendAccList))
.data;
List<ContactInfo> contactList = friendList.map((e) {
final contact = ContactInfo(
userList?.firstWhereOrNull(
(element) => element.userId == e.userId) ??
NIMUser(userId: e.userId!),
friend: e,
isInBlack: blackList?.contains(e.userId) == true);
contactMap[e.userId!] = contact;
return contact;
}).toList();
return contactList;
} else {
var res =
await Stream.fromIterable(friendResult.data!).asyncMap((e) async {
var contact = ContactInfo(
(await _getUser(e.userId!)) ?? NIMUser(userId: e.userId),
friend: e,
isInBlack: blackList?.contains(e.userId) == true);
contactMap[e.userId!] = contact;
return contact;
}).toList();
return res;
}
} else {
return List.empty();
}
}