ip_location

已弃用:ip_location 已弃用,请改用 qs_ip_location

迁移到 qs_ip_location

替换依赖

从应用的 pubspec.yaml 中移除 ip_location,改为:

dependencies:
  qs_ip_location: ^1.0.0

以上版本依据本地新包的 pubspec.yaml1.0.0),未核实 pub.flutter-io.cn 发布状态;使用托管依赖前请确认该版本已发布。新包要求 Dart ^3.11.5,请使用包含兼容 Dart SDK 的 Flutter 版本。

更新依赖后执行:

flutter pub get

更新导入和调用

将旧的 package:ip_location/ip_location.dart 和模型导入替换为以下入口,它同时导出 QsIpLocationModel

import 'package:qs_ip_location/qs_ip_location.dart';

Future<void> loadIpLocation() async {
  final QsIpLocationModel? location = await QsIpLocation.getIpLocation();
  if (location == null) {
    print('暂时无法获取 IP 信息');
    return;
  }

  print('IP:${location.ip}');
  print('国家:${location.countryName}');
  print('城市:${location.cityName}');
}

如果在 runApp() 之前调用,请先执行 WidgetsFlutterBinding.ensureInitialized()。平台配置及完整使用说明见 qs_ip_location 文档

更新类型和字段

旧 API 新 API
IpLocation QsIpLocation
IpLocationModel QsIpLocationModel
country countryName
region regionCode
city cityName
query ip

statuszipisporgas 已移除,不提供兼容别名。请删除或调整使用这些字段的业务逻辑。

模型反序列化由 IpLocationModel.fromJson(data) 改为命名参数形式:

final model = QsIpLocationModel.fromJson(
  json: <String, dynamic>{
    'ip': '8.8.8.8',
    'countryName': 'United States',
  },
);
final json = model.toJson();

新模型构造参数均为可选命名参数,字段均为只读且可空。迁移时请检查字段访问及空值处理。