Dart Requires Chilkat v11.0.0+
Dart
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
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';
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
final url = CkStringBuilder();
url.append('http://ip-api.com/json/');
url.append(http.urlEncode(ipAddress));
final parameters = CkJsonObject();
parameters.updateString('fields', 'status,message,query,country,countryCode,regionName,city,lat,lon,timezone');
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;
}
// Save these values if the application will make further requests.
final remaining = response.getHeaderField('X-Rl');
if (remaining == '0') {
print('Rate limit reached. Wait ${response.getHeaderField('X-Ttl')} seconds before the next request.');
}
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.stringOfEquals('status', 'success', true)) {
print('The service could not complete the lookup.');
if (json.jsonTypeOf('message') == 1) {
print(json.stringOf('message'));
}
return;
}
if ((json.jsonTypeOf('query') != 1) || (json.stringOfEquals('query', '', true))) {
print('The response did not contain the expected IP address.');
return;
}
print('IP address: ${json.stringOf('query')}');
if ((json.jsonTypeOf('country') == 1) && (!json.stringOfEquals('country', '', true))) {
print('Country: ${json.stringOf('country')}');
} else {
print('Country: unavailable');
}
if ((json.jsonTypeOf('countryCode') == 1) && (!json.stringOfEquals('countryCode', '', true))) {
print('Country code: ${json.stringOf('countryCode')}');
} else {
print('Country code: unavailable');
}
if ((json.jsonTypeOf('regionName') == 1) && (!json.stringOfEquals('regionName', '', true))) {
print('Region: ${json.stringOf('regionName')}');
} 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('timezone') == 1) && (!json.stringOfEquals('timezone', '', true))) {
print('Time zone: ${json.stringOf('timezone')}');
} else {
print('Time zone: unavailable');
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((json.jsonTypeOf('lat') == 2) && (json.jsonTypeOf('lon') == 2)) {
print('Coordinates: ${json.stringOf('lat')}, ${json.stringOf('lon')}');
} else {
print('Coordinates: unavailable');
}
}