Flutter美国邮编地址查询插件usa_zipcode_address_lookup的使用
Flutter美国邮编地址查询插件usa_zipcode_address_lookup的使用
United States Zip Code Lookup
一个用于根据美国邮政编码检索详细位置信息的Flutter包。该插件通过向云端服务器发送请求来获取与给定邮政编码相关的地址、州名、城市名、本地名、区号和州代码。
该插件适用于需要准确美国邮政编码位置信息的应用程序。
Gallery

Features
- 根据美国邮政编码检索位置详情。
- 返回的信息包括:
- 地址
- 州名
- 城市名
- 本地名
- 区号
- 州代码
Supported Devices
Android | iOS | MacOS | Windows | Linux | Web |
---|---|---|---|---|---|
✅ | ✅ | (待测试) | (待测试) | (待测试) | ❌ |
Installation
在你的项目pubspec.yaml
文件中添加此包:
dependencies:
usa_zipcode_address_lookup: ^1.0.0
然后运行:
flutter pub get
Usage
Import the Package
import 'package:usa_zipcode_address_lookup/usa_zipcode_address_lookup.dart';
Example
要检索位置详情,创建USAZipCodeAddressLookup
类(或类似)的一个静态实例,并调用带有邮政编码参数的searchZipcode
方法。
示例如下:
ZipCodeLocation locationModel = await USAZipCodeAddressLookup.searchZipcode(randomZipCode);
Sample Output
给定一个邮政编码,输出将包含以下信息:
Address: 320 MAIN ST
State Name: Kansas
City Name: SUMMERFIELD
Local Name: SUMMERFIELD
Area Code: 4J
State Code: KS
License
Contributions
欢迎贡献!请提交拉取请求或报告问题。
完整示例Demo
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:usa_zipcode_address_lookup/usa_zipcode_address_lookup.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: '美国邮政编码地址示例',
theme: ThemeData(
primarySwatch: Colors.blue,
),
themeMode: ThemeMode.system,
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home();
[@override](/user/override)
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
// 邮政编码列表
List<String> zipcodes = [
"66541",
"54433",
"00601",
"66543",
"72122",
"55424",
"62345"
];
String randomZipCode = "";
String zipcodeInformation = "[无信息(点击刷新)]";
bool isLoading = false;
[@override](/user/override)
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(title: const Text("美国邮政编码地址查询")),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: size.width,
child: const Text(
"查询任何美国邮政编码",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 22),
),
),
SizedBox(
width: size.width,
child: const Text(
"美国邮政编码地址查找器",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 10),
),
),
const SizedBox(height: 20),
isLoading
? const CircularProgressIndicator()
: Column(
children: [
const SizedBox(height: 3),
Text(
randomZipCode.toString(),
style: const TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
const Text(
"邮政编码信息",
style:
TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
Text(
zipcodeInformation,
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.normal),
),
],
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
setState(() {
isLoading = true;
});
// 从列表中随机选择邮政编码
Random random = Random();
randomZipCode = zipcodes[random.nextInt(zipcodes.length)];
ZipCodeLocation objec =
await USAZipCodeAddressLookup.searchZipcode(randomZipCode);
updateScreen(objec);
},
// 设置字体大小为20
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Color.fromARGB(255, 12, 115, 199)),
padding: MaterialStateProperty.all(const EdgeInsets.all(13)),
textStyle: MaterialStateProperty.all(
const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
),
child: const Text("查询邮政编码地址")),
],
),
);
}
void updateScreen(ZipCodeLocation objec) {
setState(() {
zipcodeInformation = "";
zipcodeInformation += "邮政编码 : ${objec.zipCode}\n";
zipcodeInformation += "城市 : ${objec.city}\n";
zipcodeInformation += "州代码 : ${objec.stateCode}\n";
zipcodeInformation += "州名 : ${objec.stateName}\n";
zipcodeInformation += "本地名 : ${objec.localName}\n";
zipcodeInformation += "地址 : ${objec.address}\n";
zipcodeInformation += "区号 : ${objec.areaCode}\n";
isLoading = false;
});
}
}
更多关于Flutter美国邮编地址查询插件usa_zipcode_address_lookup的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter美国邮编地址查询插件usa_zipcode_address_lookup的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
usa_zipcode_address_lookup
是一个用于在 Flutter 应用中查询美国邮编地址的插件。它可以帮助开发者通过美国邮编(ZIP Code)快速获取对应的地址信息。以下是如何使用这个插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 usa_zipcode_address_lookup
插件的依赖:
dependencies:
flutter:
sdk: flutter
usa_zipcode_address_lookup: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:usa_zipcode_address_lookup/usa_zipcode_address_lookup.dart';
3. 使用插件查询地址
你可以使用 UsaZipcodeAddressLookup
类来查询美国邮编对应的地址信息。以下是一个简单的示例:
class ZipCodeLookupExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('USA ZIP Code Lookup'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
String zipCode = '90210'; // 你想要查询的 ZIP Code
try {
AddressInfo addressInfo = await UsaZipcodeAddressLookup.lookup(zipCode);
print('City: ${addressInfo.city}');
print('State: ${addressInfo.state}');
print('Postal Code: ${addressInfo.postalCode}');
print('Country: ${addressInfo.country}');
} catch (e) {
print('Error: $e');
}
},
child: Text('Lookup ZIP Code'),
),
),
);
}
}
4. 处理查询结果
UsaZipcodeAddressLookup.lookup
方法返回一个 AddressInfo
对象,其中包含以下字段:
city
: 城市名称state
: 州名称postalCode
: 邮政编码country
: 国家名称
你可以根据需要使用这些字段来显示或处理地址信息。
5. 处理错误
如果输入的邮编无效或查询失败,lookup
方法会抛出一个异常。你可以使用 try-catch
块来捕获并处理这些错误。
6. 运行应用
现在你可以运行你的 Flutter 应用,并测试邮编查询功能。
注意事项
- 确保你输入的邮编是有效的美国邮编。
- 插件可能依赖于外部 API,因此需要网络连接才能正常工作。
示例代码
以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:usa_zipcode_address_lookup/usa_zipcode_address_lookup.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: ZipCodeLookupExample(),
);
}
}
class ZipCodeLookupExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('USA ZIP Code Lookup'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
String zipCode = '90210'; // 你想要查询的 ZIP Code
try {
AddressInfo addressInfo = await UsaZipcodeAddressLookup.lookup(zipCode);
print('City: ${addressInfo.city}');
print('State: ${addressInfo.state}');
print('Postal Code: ${addressInfo.postalCode}');
print('Country: ${addressInfo.country}');
} catch (e) {
print('Error: $e');
}
},
child: Text('Lookup ZIP Code'),
),
),
);
}
}