iso_countries 1.0.3
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
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;
setState(() {
countryList = countries;
});
}
// 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(() {
countryList = countries;
});
}
1.0.0 #
Initial release.
1.0.1 #
Minor changes
1.0.2 #
Minor changes
1.0.3 #
Fix imports
iso_countries_example #
Demonstrates how to use the iso_countries plugin.
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Country> countryList;
@override
void initState() {
super.initState();
prepareDefaultCountries();
}
// 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;
setState(() {
countryList = countries;
});
}
// 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(() {
countryList = countries;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: prepareLocaleSpecificCountries,
child: Text("fr-fr"),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
FlatButton(
textColor: Colors.white,
onPressed: prepareDefaultCountries,
child: Text("Default"),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
],
title: const Text('Plugin example app'),
),
body: _buildListOfCountries()
),
);
}
Widget _buildListOfCountries(){
return ListView.builder(itemBuilder: (BuildContext context, int index){
final Country country = countryList[index];
return ListTile(
title: Text(country.name),
subtitle: Text(country.countryCode),
);
},
itemCount: countryList != null ? countryList.length : 0,);
}
}
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
iso_countries: ^1.0.3
2. Install it
You can install packages from the command line:
with Flutter:
$ flutter pub get
Alternatively, your editor might support flutter pub get
.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
import 'package:iso_countries/iso_countries.dart';
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
20
|
Health:
Code health derived from static analysis.
[more]
|
99
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
59
|
We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.6.1
- pana: 0.12.21
- Flutter: 1.9.1+hotfix.6
Platforms
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Health suggestions
Fix lib/iso_countries.dart
. (-1.49 points)
Analysis of lib/iso_countries.dart
reported 3 hints:
line 16 col 36: Name non-constant identifiers using lowerCamelCase.
line 29 col 32: Name non-constant identifiers using lowerCamelCase.
line 30 col 7: Name non-constant identifiers using lowerCamelCase.
Dependencies
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.1.0 <3.0.0 | ||
flutter | 0.0.0 | ||
Transitive dependencies | |||
collection | 1.14.11 | 1.14.12 | |
meta | 1.1.7 | 1.1.8 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |