IP 地址
此包可让您获取公共 IP 地址以及有关它的许多其他信息
功能
您可以获取用户的公共 IP 地址或有关特定 IP 地址的许多其他信息,例如:
- 国家名称
- 时区
- 纬度
- 经度
- 城市名称
- 地区名称
- 地区代码
- 邮政编码
- 组织或互联网服务提供商
- 洲代码
- ISO 3166-1 alpha-2 两字母国家代码
- ISO 3166-1 alpha-3 三字母国家代码
- UTC 时间偏移量
- ASN
入门
将此添加到您的依赖项
dependencies:
ip_address: ^1.0.0
并导入
import 'package:ip_address/ip_address.dart';
用法
要获取您的 IP 地址,只需使用 IpAddress.getIp()
import 'package:ip_address/ip_address.dart';
void main() async {
var ip = await IpAddress.getIp();
print(ip);
// Sample Output : 208.67.222.222
}
别忘了 await 它,因为它是一个 Future 函数。
其他功能
- 要获取有关用户 IP 地址的所有数据,请使用
getAllData()
Map allData = await IpAddress.getAllData();
对于特定 IP,请使用 getAllDataFor('specific_ip') 方法。
- 要仅获取用户的 IP 地址,请使用
IpAddress.getIp():
String ip = await IpAddress.getIp();
// Sample Output : "208.67.222.222"
- 要获取用户 IP 的
Ipv4(IP 版本 4)地址值,请使用
IpAddress.getIpv4():
String ipv4 = await IpAddress.getIpv4();
// Sample Output : "208.67.222.222"
- 要获取用户 IP 的
Ipv6(IP 版本 6)地址值,请使用
IpAddress.getIpv6():
String ipv6 = await IpAddress.getIpv6();
// Sample Output : "2a05:dfc7:5::53"
- 要获取用户 IP 的洲代码,请使用
IpAddress.getContinentCode():
String continentCode = await IpAddress.getContinentCode();
// Sample Output : "NA"
对于特定 IP,请改用 getContinentCodeFor('specific_ip') 方法。
- 要获取用户的 IP 国家,请使用
IpAddress.getCountry():
String country = await IpAddress.getCountry();
// Sample Output : "United States"
对于特定 IP,请改用 getCountryFor('specific_ip') 方法。
- 要获取用户 IP 的 ISO 3166-1 alpha-2 两字母国家代码,请使用
IpAddress.getCountryCode():
String countryCode = await IpAddress.getCountryCode();
// Sample Output : "US"
对于特定 IP,请改用 getCountryCodeFor('specific_ip') 方法。
- 要获取用户 IP 的 ISO 3166-1 alpha-3 三字母国家代码,请使用
IpAddress.getCountryCode3():
String countryCode3 = await IpAddress.getCountryCode3();
// Sample Output : "USA"
对于特定 IP,请改用 getCountryCode3For('specific_ip') 方法。
- 要获取用户 IP 的纬度,请使用
IpAddress.getLatitude():
double latitude = await IpAddress.getLatitude();
// Sample Output : 37.7697
对于特定 IP,请改用 getLatitudeFor('specific_ip') 方法。
- 要获取用户 IP 的经度,请使用
IpAddress.getLongitude():
double longitude = await IpAddress.getLongitude();
// Sample Output : -122.3933
对于特定 IP,请改用 getLongitudeFor('specific_ip') 方法。
- 要获取用户 IP 的时区,请使用
IpAddress.getTimeZone():
String timeZone = await IpAddress.getTimeZone();
// Sample Output : "America\/Los_Angeles"
对于特定 IP,请改用 getTimeZoneFor('specific_ip') 方法。
- 要获取用户 IP 的 UTC 时间偏移量,请使用
IpAddress.getOffset():
int offset = await IpAddress.getOffset();
// Sample Output : -7
对于特定 IP,请改用 getOffsetFor('specific_ip') 方法。
- 要获取用户 IP 的组织或互联网服务提供商(ASN + ISP 名称),请使用
IpAddress.getOrganization():
int organization = await IpAddress.getOrganization();
// Sample Output : "AS36692 OpenDNS, LLC"
对于特定 IP,请改用 getOrganizationFor('specific_ip') 方法。
- 要获取用户 IP 的地区名称,请使用
IpAddress.getRegion():
String region = await IpAddress.getRegion();
// Sample Output : "New York"
对于特定 IP,请改用 getRegionFor('specific_ip') 方法。
- 要获取用户 IP 的地区代码,请使用
IpAddress.getRegionCode():
String regionCode = await IpAddress.getRegionCode();
// Sample Output : "NY"
对于特定 IP,请改用 getRegionCodeFor('specific_ip') 方法。
- 要获取用户 IP 的城市名称,请使用
IpAddress.getCity():
String city = await IpAddress.getcity();
// Sample Output : "Brooklyn"
对于特定 IP,请改用 getCityFor('specific_ip') 方法。
- 要获取用户 IP 的邮政编码/邮编,请使用
IpAddress.getPostalCode():
String postalCode = await IpAddress.getPostalCode();
// Sample Output : "11213"
对于特定 IP,请改用 getPostalCodeFor('specific_ip') 方法。
附加信息
某些国家/地区没有地区和其他信息。例如,对于美国和加拿大境外的某些 IP 地址,可能找不到 region、regionCode、city 和 postalCode 等信息。