find static method

Find users' country using their current IP

Returns CountryResponse

On Error returns null

Example:

final countryIpResponse = await CountryIp.find();

print("countryIpResponse : ${countryIpResponse}");
// countryIpResponse : CountryResponse(country: United States, countryCode: US, ip: 9.9.9.9)
print("User's country code : ${countryIpResponse?.countryCode}");
// User's country code : US
print("User's country : ${countryIpResponse?.country}");
// User's country : United States
print("User's ip : ${countryIpResponse?.ip}");
// User's ip : 9.9.9.9

Implementation

static Future<CountryResponse?> find() async {
  try {
    final response = await get(Uri.https('api.country.is'));

    final Map<String, dynamic> body = jsonDecode(response.body);

    return CountryResponse.fromJson(body);
  } catch (_) {
    return null;
  }
}