用途:在 Flutter 应用中执行 HTTP 请求并处理 JSON 序列化,适用于对接 REST API 或解析外部结构化数据。
主要能力与准则:强制使用 HTTPS(iOS/Android 默认禁用明文 HTTP,调试需配置 network_security_config.xml 与 NSAppTransportSecurity);用 Uri.https(authority, unencodedPath, [queryParameters]) 安全构造 URL;始终校验 http.Response.statusCode,200/201 视为成功,其他状态码抛出显式异常而非返回 null;超过 16ms 的 JSON 解析用 compute() 移到后台 isolate;对接 LLM 时在系统提示中指定严格 JSON schema 并将响应 MIME 设为 application/json。
使用方式:HTTP 操作流程为添加 http 包、配置平台权限(AndroidManifest.xml 的 Internet 权限、macOS .entitlements)、构造 Uri、执行方法、校验并解析。GET 用 http.get(uri);POST 用 http.post(uri, headers: {...}, body: jsonEncode(data)),Content-Type 为 application/json; charset=UTF-8;PUT 与 DELETE 类似。序列化策略:小型原型用 dart:convert 手写 fromJson/toJson;生产复杂模型用 json_serializable 代码生成,命令为 flutter pub add json_annotation、flutter pub add -d build_runner json_serializable,并运行 dart run build_runner build --delete-conflicting-outputs。大 JSON 解析:编写顶层或静态函数接收 String 返回 Dart 对象,在 fetch 中通过 compute() 调用。
限制:资料未提供具体性能数据、收费、平台支持范围或错误重试策略等细节。