GithubHelp home page GithubHelp logo

app2sales / flutter_iso_countries Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anoop4real/flutter_iso_countries

0.0 0.0 0.0 104 KB

A Flutter plugin to fetch list of countries and their code from device

License: MIT License

Ruby 10.92% Objective-C 2.09% Kotlin 23.79% Dart 44.69% Swift 18.51%

flutter_iso_countries's Introduction

iso_countries

A plugin for fetching ISOCountries data from device OS. Name of countries can be obtained in different languages by passing in the language format. ("fr-fr", "de-de"). No values are HARDCODED, country details are fetched from OS.

Android: API level 21 required

iso_countries

Update

Version 2.1.0 Android Embedding V2

Update

Version 2.0.0 updated to support null safety

Usage

For detailed use, see the example.

Fetch Default (English)

    List<Country>? countries;
    try {
      countries = await IsoCountries.iso_countries;
    } on PlatformException {
      countries = null;
    }

Fetch based on Language


    List<Country>? countries;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      // If you need country names in a specific language please pass language code sample
      // fr-fr, en-en, de-de... IMPORTANT: In Android there seem to be some issue with case
      // so passing fr-FR wont work
      countries = await IsoCountries.iso_countries_for_locale('fr-fr');
    } on PlatformException {
      countries = null;
    }
    

Usage in Widget as function

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> prepareDefaultCountries() async {
    List<Country>? countries;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      countries = await IsoCountries.iso_countries;
    } on PlatformException {
      countries = null;
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) {
      return;
    }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> prepareLocaleSpecificCountries() async {
    List<Country>? countries;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      // If you need country names in a specific language please pass language code sample
      // fr-fr, en-en, de-de... IMPORTANT: In Android there seem to be some issue with case
      // so passing fr-FR wont work
      countries = await IsoCountries.iso_countries_for_locale('fr-fr');
    } on PlatformException {
      countries = null;
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) {
      return;
    }

    setState(() {
      if (countries != null) {
        countryList = countries;
      }
    });
  }
  

New API Added

Passing in a country code( 2 letter) and an optional localeIdentifier( eg 'de-de', 'fr-fr'), you can get a country object with a translated name.

// Platform messages are asynchronous, so we initialize in an async method.
// IMPORTANT: Make sure the country code passed in is valid, in Android passing
// in a wrong country code, returns the country name as passed in country code not sure why.
Future<void> getCountryForCodeWithIdentifier(
    String code, String localeIdentifier) async {
  // Platform messages may fail, so we use a try/catch PlatformException.
  try {
    country = await IsoCountries.iso_country_for_code_for_locale(code,
        locale_identifier: localeIdentifier);
  } on PlatformException {
    country = null;
  }
  // If the widget was removed from the tree while the asynchronous platform
  // message was in flight, we want to discard the reply rather than calling
  // setState to update our non-existent appearance.
  if (!mounted) {
    return;
  }

  setState(() {
    print(country?.name);
  });
}

Note

If you are getting any error related to pods while running iOS example, then please delete the podfile and podfile.lock and re-run.

flutter_iso_countries's People

Contributors

adrienbaron avatar anoop4real avatar siloebb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.