I have a Blog App where I write Bussiness Content for Global as well as some country-specific content. Like Special posts for India, Indonesia, etc.
That's why I need a way to find from which country the user is to provide specific content.
But, I don't want to use the GeoLocator Plugin because It will ask the User for location. I only want the user's country, not the specific location but the permission dialog didn't specify that which can make the user anxious about why this app needs Location Permission. That's why I want a way to get user's Country without an implicit permission dialog asking for their location.
I have tried the following ways but none of them gave the exact country name.
import 'dart:io' show Platform;
String localeName = Platform.localeName;
// this returns language device is using which mostly comes us en_US so this is of no use
Locale myLocale = Localizations.localeOf(context);
// This returned the US as the country despite the App being opened and used in India.
Well, I finally found an easy and simple way to get users' country.
This link returns the Users' location in a JSON format. So All one has to do now, is to make a Get request to the above link/API and decode the JSON to Map.
For that First Install HTTP Package:
Pubspec.yaml:
dependencies:
http:
Code:
import 'package:http/http.dart' as http;
import 'dart:convert';
Response data = await http.get('http://ip-api.com/json');
Map data = jsonDecode(data.body);
String country = data['country'];
EDIT: As mentioned by good people,
Free Version Limit is 45 HTTP requests per second from an IP address.