Sample code for 30+ languages & platforms
Dart

ipstack.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST API.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final http = CkHttp();

  // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
  final String jsonStr;
  try {
    jsonStr = http.quickGetStr('http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final json = CkJsonObject();
  json.emitCompact = false;
  json.load(jsonStr);

  print(json.emit());

  // Sample output:
  // Use this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "ip": "149.250.207.170",
  //   "type": "ipv4",
  //   "continent_code": "EU",
  //   "continent_name": "Europe",
  //   "country_code": "DE",
  //   "country_name": "Germany",
  //   "region_code": null,
  //   "region_name": null,
  //   "city": null,
  //   "zip": null,
  //   "latitude": 51.2993,
  //   "longitude": 9.491,
  //   "location": {
  //     "geoname_id": null,
  //     "capital": "Berlin",
  //     "languages": [
  //       {
  //         "code": "de",
  //         "name": "German",
  //         "native": "Deutsch"
  //       }
  //     ],
  //     "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
  //     "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
  //     "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
  //     "calling_code": "49",
  //     "is_eu": true
  //   }
  // }

  var i = 0;
  var code = '';
  var name = '';
  var native = '';

  final ip = json.stringOf('ip');
  final vType = json.stringOf('type');
  final continentCode = json.stringOf('continent_code');
  final continentName = json.stringOf('continent_name');
  final countryCode = json.stringOf('country_code');
  final countryName = json.stringOf('country_name');
  final regionCode = json.stringOf('region_code');
  final regionName = json.stringOf('region_name');
  final city = json.stringOf('city');
  final zip = json.stringOf('zip');
  final latitude = json.stringOf('latitude');
  final longitude = json.stringOf('longitude');
  final locationGeonameId = json.stringOf('location.geoname_id');
  final locationCapital = json.stringOf('location.capital');
  final locationCountryFlag = json.stringOf('location.country_flag');
  final locationCountryFlagEmoji = json.stringOf('location.country_flag_emoji');
  final locationCountryFlagEmojiUnicode = json.stringOf('location.country_flag_emoji_unicode');
  final locationCallingCode = json.stringOf('location.calling_code');
  i = 0;
  final countI = json.sizeOfArray('location.languages');
  while (i < countI) {
    json.i = i;
    code = json.stringOf('location.languages[i].code');
    name = json.stringOf('location.languages[i].name');
    native = json.stringOf('location.languages[i].native');
    i++;
  }
}