getFriendsNearby method

Future<List<Friend>> getFriendsNearby(
  1. int? page,
  2. int? size
)

Implementation

Future<List<Friend>> getFriendsNearby(int? page, int? size) async {
  //
  double? latitude = BytedeskUtils.getLatitude();
  double? longtitude = BytedeskUtils.getLongtitude();
  //
  // final addressUrl =
  //     '$baseUrl/api/friend/nearby/query?lat=$latitude&lng=$longtitude&page=$page&size=$size&client=$client';
  final addressUrl = BytedeskUtils.getHostUri('/api/friend/nearby/query', {
    'lat': latitude,
    'lng': longtitude,
    'page': page.toString(),
    'size': size.toString(),
    'client': client
  });
  debugPrint("address Url $addressUrl");
  final initResponse =
      await httpClient.get(addressUrl, headers: getHeaders());

  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson =
      json.decode(utf8decoder.convert(initResponse.bodyBytes));
  debugPrint("responseJson $responseJson");
  // 判断token是否过期
  if (responseJson.toString().contains('invalid_token')) {
    bytedeskEventBus.fire(InvalidTokenEventBus());
  }

  List<Friend> friends = (responseJson['data']['searchHits'] as List<dynamic>)
      .map((item) => Friend.fromElasticJson(item['content']))
      .toList();

  // List<Friend> friends = (responseJson['data']['content'] as List<dynamic>)
  //     .map((item) => Friend.fromJson(item))
  //     .toList();

  return friends;
}