getGeoLocation method

Future<String?> getGeoLocation(
  1. String ip
)

Returns the country for the given IP address.

This method sends a request to http://ip-api.com/json/$ip to get the country for the given IP address. If the request is successful, it returns the country, otherwise it returns null.

  • Parameters:

    • ip: The IP address to get the country for.
  • Returns: The country for the given IP address, or null if the request fails.

Implementation

Future<String?> getGeoLocation(String ip) async {
  final url = 'http://ip-api.com/json/$ip';
  final response = await http.get(Uri.parse(url));

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['country'];
  }
  return null;
}