Dart Requires Chilkat v11.0.0+
Dart
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
final ipAddress = '8.8.8.8';
final http = CkHttp();
http.requireSslCertVerify = true;
http.connectTimeout = 10;
http.readTimeout = 30;
http.accept = 'application/json';
final apiKey = 'YOUR_API_KEY';
final url = CkStringBuilder();
url.append('https://api.ipdata.co/');
url.append(http.urlEncode(ipAddress));
final parameters = CkJsonObject();
parameters.updateString('api-key', apiKey);
final response = CkHttpResponse();
success = true;
try {
http.httpParams('GET', url.getAsString(), parameters, response);
} on ChilkatException {
success = false;
}
if (response.statusCode == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
print('No HTTP response. Check connectivity, DNS, and TLS configuration.');
return;
}
final json = CkJsonObject();
var parsed = true;
try {
json.load(response.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!success) || ((response.statusCode < 200) || (response.statusCode >= 300))) {
print('Lookup failed. HTTP ${response.statusCode}');
if (parsed) {
if (json.jsonTypeOf('message') == 1) {
print(json.stringOf('message'));
}
}
return;
}
if (!parsed) {
print('Expected a JSON object in the lookup response.');
return;
}
if ((json.jsonTypeOf('ip') != 1) || (json.stringOfEquals('ip', '', true))) {
print('The response did not contain the expected IP address.');
return;
}
print('IP address: ${json.stringOf('ip')}');
if ((json.jsonTypeOf('country_name') == 1) && (!json.stringOfEquals('country_name', '', true))) {
print('Country: ${json.stringOf('country_name')}');
} else {
print('Country: unavailable');
}
if ((json.jsonTypeOf('country_code') == 1) && (!json.stringOfEquals('country_code', '', true))) {
print('Country code: ${json.stringOf('country_code')}');
} else {
print('Country code: unavailable');
}
if ((json.jsonTypeOf('region') == 1) && (!json.stringOfEquals('region', '', true))) {
print('Region: ${json.stringOf('region')}');
} else {
print('Region: unavailable');
}
if ((json.jsonTypeOf('city') == 1) && (!json.stringOfEquals('city', '', true))) {
print('City: ${json.stringOf('city')}');
} else {
print('City: unavailable');
}
if ((json.jsonTypeOf('time_zone.name') == 1) && (!json.stringOfEquals('time_zone.name', '', true))) {
print('Time zone: ${json.stringOf('time_zone.name')}');
} else {
print('Time zone: unavailable');
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((json.jsonTypeOf('latitude') == 2) && (json.jsonTypeOf('longitude') == 2)) {
print('Coordinates: ${json.stringOf('latitude')}, ${json.stringOf('longitude')}');
} else {
print('Coordinates: unavailable');
}
if ((json.jsonTypeOf('asn.asn') == 1) && (!json.stringOfEquals('asn.asn', '', true))) {
print('ASN: ${json.stringOf('asn.asn')}');
} else {
print('ASN: unavailable');
}
if ((json.jsonTypeOf('asn.name') == 1) && (!json.stringOfEquals('asn.name', '', true))) {
print('Network organization: ${json.stringOf('asn.name')}');
} else {
print('Network organization: unavailable');
}
}