queryOwnIpInfo static method

Future queryOwnIpInfo({
  1. IpQueryFormat format = IpQueryFormat.json,
})

Query your own IP address information.

Optionally specify the response format (defaults to JSON).

Returns an IpInfo object for JSON format, or a raw response string for other formats.

final info = await IpQuery.queryOwnIpInfo();
print('Country: ${info.location?.country}');

Implementation

static Future<dynamic> queryOwnIpInfo(
    {IpQueryFormat format = IpQueryFormat.json}) async {
  final response = await http
      .get(Uri.parse(_baseUrl + '?format=${_formatToString(format)}'));
  if (response.statusCode != 200) {
    throw Exception('Failed to fetch own IP info: ${response.statusCode}');
  }
  if (format == IpQueryFormat.json) {
    return IpInfo.fromJson(jsonDecode(response.body));
  } else {
    return response.body;
  }
}