fetchUserInfoById method Null safety

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

Gets user attributes of the specified users.

Param userIds The username array.

Param expireTime The time period(seconds) when the user attibutes in the cache expire. If the interval between two calles 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 {@link EMError}.

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;
  }
}