0% found this document useful (0 votes)
38 views1 page

Get Location by Ip

This document describes a global function that returns the country name based on the device's IP address. It uses an external API to get location data by IP. The function first checks secure storage for a cached country name, and if not found, calls the API, caches the result, and returns the country name. It provides an example response and function implementation.

Uploaded by

Yasser Dz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views1 page

Get Location by Ip

This document describes a global function that returns the country name based on the device's IP address. It uses an external API to get location data by IP. The function first checks secure storage for a cached country name, and if not found, calls the API, caches the result, and returns the country name. It provides an example response and function implementation.

Uploaded by

Yasser Dz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Global function to get the country :

Futur<String> getCountryNameByIP();

Call this function in the main


function of Farmer App and the
Advisor app.

How it works ?

1
 apiip API returns more than 40 unique data
points, such as location data, and that what we
2
need.
in the function body we test if the country
to use this service,we have to get the ip Name is stored in Storage using
address of the device and the accessKey and FlutterSecureStorage, if its then return the
the put them in a url like this: stored data ,
http://apiip.net/api/check?ip=67.250.186.196
&accessKey=ef7b5d33-cbff-4a3e-841f-c414a72fb3c0 If not ,then we call the uri and get a
response,
Check the response status if its successfully
the key we put it in the global variables connected (status code = 200 ) then get then
accessKey=« ef7b5d33-cbff-4a3e-841f- decode the response body to get the country
c414a72fb3c0 »;
Name and store it in storage , else throw an
exception,
and the ip address we get it using this url:
'https://api.ipify.org'

Example of response
{
"ip": "67.250.186.196",
"continentCode": "NA",
"continentName": "North America",
"countryCode": "US",
"countryName": "United States",
"countryNameNative": "United States",
"officialCountryName": "United States of America",
"regionCode": "NY",
"regionName": "New York",
"cityGeoNameId": 5128581,
"city": "New York",
"postalCode": "10004",
"latitude": 40.7123,
"longitude": -74.0068,
"capital": "Washington D.C.",
"phoneCode": "1",
"countryFlagEmoj": "🇺🇸 ",
"countryFlagEmojUnicode": "U+1F1FA U+1F1F8",
"borders": [
"CAN",
"MEX"
],
"topLevelDomains": [
".us"
]
}

Example of function

Future<String> getIpAddress() async {


var response = await http.get(Uri.parse('http://api.ipify.org'));
if (response.statusCode == 200) {
print(response.body);
return response.body;
} else {
throw Exception('Failed to get IP address');
}
}

Future<String> getCountryCodeFromIP() async {


String ipAddress= await getIpAddress();
final storage = FlutterSecureStorage();
final storedData = await storage.readAll();
if (storedData.containsKey('ip_data')) {
final ipData = storedData['ip_data'];
print('ip data');
print(ipData);
return ipData??'';
}
else {
final response = await http.get(Uri.parse('http://apiip.net/api/check?ip=$ipAddress &accessKey=ef7b5d33-cbff-4a3e-841f-c414a72fb3c0'));
if (response.statusCode == 200) {
print(json.decode(response.body)['countryName']);
return json.decode(response.body)['countryName'];
} else {
throw Exception('Failed to get IP address data');
}
}
// This function first checks if the IP address data is stored in flutter_secure_storage.
// If the data is stored, it retrieves the country code and returns it.
// If the data is not stored, it makes a request to the IP address data API with the provided API

You might also like