fetchUserInfoById method

Future<Map<String, EMUserInfo>> fetchUserInfoById(
  1. List<String> userIds, {
  2. int expireTime = 0,
})

~english Gets user attributes of the specified users.

Param userIds The username array.

Param expireTime The time period(seconds) when the user attributes in the cache expire. If the interval between two callers is less than or equal to the value you set in the parameter, user attributes are obtained directly from the local cache; otherwise, they are obtained from the server. For example, if you set this parameter to 120(2 minutes), once this method is called again within 2 minutes, the SDK returns the attributes obtained last time.

Return A map that contains key-value pairs where the key is the user ID and the value is user attributes.

Throws A description of the exception. See EMError. ~end

~chinese 根据用户 ID,获取指定用户的用户属性。

Param userIds 用户 ID 数组。

Param expireTime 获取的用户属性到期时间。如果在到期时间内再次调用该方法,则 SDK 直接返回上次获取到的缓存数据。例如,将该参数设为 120,即 2 分钟,则如果你在 2 分钟内再次调用该方法获取用户属性,SDK 仍将返回上次获取到的属性。否则需从服务器获取。

Return 返回 key-value 格式的 Map 类型数据,key 为用户 ID,value 为用户属性。

Throws 如果有方法调用的异常会在这里抛出,可以看到具体错误原因。请参见 EMError。 ~end

Implementation

Future<Map<String, EMUserInfo>> fetchUserInfoById(
  List<String> userIds, {
  int expireTime = 0,
}) async {
  List<String> needReqIds = userIds
      .where((element) =>
          !_effectiveUserInfoMap.containsKey(element) ||
          (_effectiveUserInfoMap.containsKey(element) &&
              DateTime.now().millisecondsSinceEpoch -
                      _effectiveUserInfoMap[element]!.expireTime >
                  expireTime * 1000))
      .toList();
  Map<String, EMUserInfo> resultMap = Map();

  userIds.forEach((element) {
    if (_effectiveUserInfoMap.containsKey(element)) {
      resultMap[element] = _effectiveUserInfoMap[element]!;
    }
  });
  if (needReqIds.length == 0) {
    return resultMap;
  }

  Map req = {'userIds': needReqIds};
  Map result =
      await _channel.invokeMethod(ChatMethodKeys.fetchUserInfoById, req);

  try {
    EMError.hasErrorFromResult(result);
    result[ChatMethodKeys.fetchUserInfoById]?.forEach((key, value) {
      EMUserInfo eUserInfo = EMUserInfo.fromJson(value);
      resultMap[key] = eUserInfo;
      _effectiveUserInfoMap[key] = eUserInfo;
    });
    return resultMap;
  } on EMError catch (e) {
    throw e;
  }
}