搜索插件、代码仓与 APP 分类

插件详情v1.0.2

rest_countries_data

一个 Dart 包,作为 REST Countries API 的包装器,提供对国家数据的便捷访问。

AndroidiOSmacOSWindowsLinux
近 30 天下载
15
喜欢数
29
Pub 分数
150

安装插件

flutter pub add rest_countries_data

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,通过封装 REST Countries API 简化对全球国家数据的访问。它提供了一个简洁、易于使用的接口,用于获取有关国家的详细信息,包括名称、旗帜、边界、货币等。专为需要在 Flutter 应用中使用可靠且最新国家数据的开发者设计,支持多种平台并减少样板代码。该包内部处理 HTTP 请求,并返回结构化数据,实现无缝集成。

适合场景

  • 在旅行应用中获取国家详情
  • 构建语言或货币选择器
  • 显示国家旗帜和地图
  • 创建一个全球新闻或人口统计仪表板
  • 实现用户位置选择

主要特点

  • 简单 API 包装器
  • 支持多个平台
  • 返回结构化的国家数据
  • 内部处理HTTP请求
  • 与 Flutter 的轻松集成

适合对象

  • Flutter 开发人员构建国际应用
  • 需要国家元数据的应用程序
  • 教育或参考应用程序
  • 旅行和本地化工具

使用建议

  • 需要互联网连接
  • 仅限 REST 国家 API 数据
  • 不包含离线缓存
  • 依赖外部API的可用性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

rest_countries_data

rest_countries_data Logo


A Dart wrapper around the REST Countries v3.1 API that provides a fully-typed, developer-friendly interface for accessing detailed country data.


✨ Features

  • Get all countries with selected fields (limited to 10 fields)
  • Query countries by:
    • Capital
    • Full name or partial name
    • ISO 3166-1 code or list of codes
    • Region / Subregion
    • Language / Currency / Demonym
    • Translation
    • Independence status
  • Built-in CountryModel to map the API response
  • Enum-based CountryFields selector for fine-grained queries
  • Clean separation of concerns via Repository, API and Domain layers

🚀 Getting Started

1. Install the Package

Add to your pubspec.yaml:

dependencies:
  rest_countries_data: ^1.0.0

Then run:

# For Dart users
dart pub get

# For Flutter users
flutter pub get
2. Import It
import 'package:rest_countries_data/rest_countries_data.dart';

📦 Usage

Fetch All Countries with Selected Fields
final countries = await RestCountries.getAllCountries(
  fields: [CountryFields.name, CountryFields.capital, CountryFields.region],
);

Field Limitation ⚠️

When using the fields parameter in methods like getAllCountries, the maximum number of allowed fields is 10. This limit is enforced by the REST Countries API.

Also note: Whenever you specify the fields property, the properties that will be available in the returned CountryModel depend entirely on which CountryFields you request. If you omit a field, the corresponding property in CountryModel may be null.

Get Countries by Capital
final country = await RestCountries.getCountryByCapital(capital: 'Paris');
Get a Country by ISO Code
final country = await RestCountries.getCountryByCode(code: 'NG');
Get Countries by Currency
final countries = await RestCountries.getCountriesByCurrency(currency: 'USD');
Get Countries by Region
final countries = await RestCountries.getCountriesByRegion(region: 'Europe');
Get Countries by SubRegion
final countries = await RestCountries.getCountriesBySubRegion(subRegion: 'Eastern Africa');
Get Country by Full Name
final country = await RestCountries.getCountryByFullName(fullName: 'Federal Republic of Nigeria');
Get Countries by Language, Demonym, or Translation
final countries = await RestCountries.getCountriesByLanguage(language: 'en');
final countries = await RestCountries.getCountriesByDemonym(demonym: 'Nigerian');
final countries = await RestCountries.getCountriesByTranslation(translation: 'Niger');
Get Countries by Independence Status
final countries = await RestCountries.getCountriesByIndependentStatus(
  independent: true,
  fields: [CountryFields.name, CountryFields.flag],
);

📚 Data Model

CountryModel (Simplified)
class CountryModel {
  final Name? name;
  final List<String>? capital;
  final String? region;
  final String? subregion;
  final Map<String, dynamic>? flags;
  // ...
}

class Name {
  final String common;
  final String official;
  final Map<String, NativeName>? nativeName;
}
CountryFields Enum

Use the CountryFields enum to limit which fields are returned from the API:

CountryFields.name
CountryFields.capital
CountryFields.region
CountryFields.area
CountryFields.languages
// etc.

Note: Maximum 10 fields per request for getAllCountries()

🧩 Utility Getter
getCountryPhoneNumberCode

This convenience getter is available on the CountryModel and returns the full numeric dialing code (e.g., +234 for the country Nigeria). It is constructed by combining idd.root and idd.suffixes in the CountryModel.


🔍 API Overview

Method Description
getAllCountries({required fields}) Get all countries with selected fields
getCountryByCapital({required capital}) Filter by capital city
getCountryByCode({required code}) Get a single country by ISO code
getCountriesByCodes({required codes}) Get multiple countries by ISO codes
getCountriesByCurrency({required currency}) Filter by currency
getCountriesByDemonym({required demonym}) Filter by demonym
getCountriesByLanguage({required language}) Filter by spoken language
getCountriesByRegion({required region}) Filter by region
getCountriesBySubRegion({required subRegion}) Filter by subregion
getCountriesByTranslation({required translation}) Filter by translated country name
getCountryByFullName({required fullName}) Get a country by its full name
getCountriesByName({required name}) Filter by partial or full name
getCountriesByIndependentStatus(...) Filter by independence and optionally fields
CountryModel.getCountryPhoneNumberCode Get the phone dialing code of a country (e.g. +234)

📊 Example (Run it)

import 'package:rest_countries_data/rest_countries_data.dart';

void main() async {
  final countries = await RestCountries.getCountriesByRegion(region: 'Africa');

  for (var country in countries) {
    print(country.name?.common);
  }
}

🚪 Contributing

Pull requests are welcome! If you have ideas or find bugs, feel free to open an issue.


Development Setup

After cloning the repository, install the git hooks for automatic formatting and linting:

./install-hooks.sh

This will set up a pre-commit hook that:

  • Automatically formats all Dart files
  • Runs dart analyze to check for issues
  • Prevents commits if there are analysis errors

👋 Author

Maintained by Franklin Oladipo


📄 License

BSD 3-Clause

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。