Get Device Country Code and Currency in Flutter - RRutors
Last updated Mar 02, 2022While develop applications it may require to get current country information of the device location, like name of the country, country code, country currency etc. We will use to get current country code we implement Geo-location feature then fetch country information form the current latlang values. There is an other way to fetch current country code. In this flutter example we will see how to fetch current country code of the device.
To fetch current country code we will call a network api ip-api.com, it will returns information like country name, country code, currency...
Let get started
Step 1: Create Flutter application
Step 2: Add required dependencies in pubspec.yaml file
dependencies: |
Step 3: Create a class Network to handle api calls
To call api we will use below code
Future apiRequest(Map jsonMap) async { |
Step 4: Now call method form main dart file on button click
Future getCountry() async{ |
It will returns data in key values pair, we can read data like below
var country = ${countrydata['country']}\nCountry Code = ${countrydata['countryCode']}\nTimezone = ${countrydata['timezone'] |
Complete example code to get current device country code and currency code
main dart file
import 'dart:convert'; import 'package:flutter/material.dart'; void main() { class MyApp extends StatelessWidget { // This widget is the root of your application. class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { @override return Scaffold(body: Container( });
Future getCountry() async{
|
Network. dart file
import 'package:http/http.dart' as http; class Network { Network(this.url); Future apiRequest(Map jsonMap) async { Future sendData(Map data) async { Future getData() async { |
Conclusion: In this flutter example we covered how to get current country code and currency
Article Contributed By :
|
|
|
|
2052 Views |