You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS端POST请求无响应问题排查与解决

Flutter iOS端POST请求超时问题排查与解决

问题现象

开发首个Flutter应用时,Android端与部署在Contabo服务器的后端交互所有HTTP请求均正常,但在iOS模拟器及iPhone 7 Plus、13、14真机上出现异常:

  • GET请求正常响应
  • POST请求无响应,移除timeout设置后需15-20分钟才能收到结果

排查过程

先后尝试以下操作均未解决问题:

  • 切换使用http包与dio包发送请求
  • 测试本地服务器与远程Contabo服务器
  • 尝试对请求数据进行jsonEncode或不编码处理

最终定位核心问题:POST请求中携带的base64格式图片是导致超时的根源,移除该图片字段后,POST请求响应恢复正常。

解决方案

使用flutter_image_compress包对图片进行压缩后,再转为base64格式携带到POST请求中,彻底解决了超时问题。

相关代码

原注册请求代码

singUp(String userName, String phone, String image, String locationId,
    String subLocation) async {
  var myToken = OneSignal.User.pushSubscription.id;
  await clearLoginInfo();
  var now = DateTime.now();
  var formatter = DateFormat('yyyy-MM-dd');
  String formattedDate = formatter.format(now);

  try {
    Map<String, String> requestHeaders = {
      'Accept': 'application/json',
      'Content-Type': 'application/json; charset=UTF-8',
    };
    var list = {
      'u_name': userName,
      'u_phone': phone.toString(),
      'u_image': image,
      'u_location': subLocation.toString(),
      'u_date': formattedDate.toString(),
      'u_descrption': 'u_descrption',
      'notification_token': myToken.toString()
    };
    final dio = Dio();
    final response = await dio.post(
      'http://10.0.0.2/kolshy/user_sigup.php',
      data: list,
    ).timeout(Duration(seconds: 10));
    print(response.data);

    // 注释的http包请求代码
    // final response = await http.post(
    //   Uri.parse('http://10.0.0.2/kolshy/user_sigup.php'),
    //   body: {
    //     'u_name': userName,
    //     'u_phone': phone.toString(),
    //     'u_image': image,
    //     'u_location': subLocation.toString(),
    //     'u_date': formattedDate.toString(),
    //     'u_descrption': 'u_descrption',
    //     'notification_token': myToken.toString()
    //   },
    // ).timeout(Duration(seconds: 10));
    // print('===========================================${response.body}');
    // print('===========================================${response.statusCode}');

    // if (response.statusCode == 200) {
    //   final data = jsonDecode(response.body);

    //   if (data['message'] == '1') {
    //     await loginByPhone(phone);
    //     return 1;
    //   } else if (data['message'] == '2') {
    //     // phone Exist
    //     return 2;
    //   } else if (data['message'] == '3') {
    //     // name Exist
    //     return 3;
    //   }
    // }
  } on TimeoutException {
    return 5;
  } on SocketException {
    return 4;
  }
}

图片压缩与base64转换示例

import 'dart:io';
import 'dart:convert';
import 'package:flutter_image_compress/flutter_image_compress.dart';

// 压缩图片并转为base64字符串
Future<String> compressImageToBase64(String imagePath) async {
  final compressedFile = await FlutterImageCompress.compressAndGetFile(
    imagePath,
    '${imagePath.split('.').first}_compressed.${imagePath.split('.').last}',
    quality: 50, // 可根据需求调整压缩质量,值越小压缩率越高
  );

  if (compressedFile != null) {
    final imageBytes = await compressedFile.readAsBytes();
    return base64Encode(imageBytes);
  }
  return '';
}

内容的提问来源于stack exchange,提问作者abdu idres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 17:53:15