Flutter电商App跨表数据传递问题:调用.NET Core API Post无响应
问题描述
我正在开发基于Flutter的Android电商应用,刚接触Dart。需求是从SQL Server的一个表取数据,提交到另一个表,后端用.NET Core API。目前能成功获取数据并传递给Post方法,但数据库没更新。断点触发Post方法后,代码没有执行流程,也没返回任何状态码。
调用代码
httpService.getPosts().then((value) { if (value != null) { value.forEach((element) { httpServices.addPosts( 0, element.cartProductID, element.productBrandId, element.cartUserID, element.item, element.quantity, element.price, element.totalPrice, element.discount, // element.isOrdered, element.paymentID, element.paymentMode, element.date, ); });
Get与Post方法实现
class GetOrderHttpService with ChangeNotifier { Future<List<OrderTotal>> getPosts() async { Response res = await http.get(Uri.https('********')); if (res.statusCode == 200) { List<dynamic> body = jsonDecode(res.body); List<OrderTotal> posts = body .map( (dynamic dynamic) => OrderTotal.fromJson(dynamic), ) .toList(); notifyListeners(); return posts; } else { throw "Unable to retrieve posts."; } } } Future<bool> addPosts( int orderID, int orderProductID, int productBrandId, int orderUserID, String item, int quantity, double price, double totalPrice, double discount, int paymentID, String? paymentMode, DateTime date, ) async { var response = await http.post( Uri.https('************'), body: jsonEncode({ 'orderID': orderID, 'orderProductID': orderProductID, 'productBrandId': productBrandId, 'orderUserID': orderUserID, 'item': item, 'quantity': quantity, 'price': price, 'totalPrice': totalPrice, 'discount': discount, 'paymentID': paymentID, 'paymentMode': paymentMode, 'date': date }), headers: { "Accept": "application/json", "content-type": "application/json" }); var data = response.body; if (response.statusCode == 200) { return true; } else throw Exception(); } }
排查步骤与解决方案
1. 修复DateTime序列化问题
Dart的DateTime直接序列化会生成带时区的字符串(如2024-05-20T12:34:56.789Z),.NET Core默认解析逻辑可能不兼容,导致请求卡住或参数解析失败。修改日期序列化方式:
'date': date.toIso8601String()
2. 处理异步调用的等待与错误捕获
当前forEach调用异步方法addPosts时没有等待,也没做错误捕获,可能请求抛出异常但你完全没感知。换成支持异步等待的for循环,并添加异常捕获:
httpService.getPosts().then((value) async { if (value != null) { for (var element in value) { try { bool result = await httpServices.addPosts( 0, element.cartProductID, element.productBrandId, element.cartUserID, element.item, element.quantity, element.price, element.totalPrice, element.discount, element.paymentID, element.paymentMode, element.date, ); print("提交成功: $result"); } catch (e) { print("提交失败: $e"); } } } }).catchError((e) { print("获取数据失败: $e"); });
3. 验证请求内容与后端参数匹配
- 检查.NET Core API的模型参数名称、类型和你提交的完全一致(比如
orderProductID是否同名、paymentMode是否为可空字符串) - 在
addPosts里添加日志,打印请求URL、Body和响应信息,确认请求是否正确发送:
Future<bool> addPosts(...) async { final requestBody = jsonEncode({ 'orderID': orderID, 'orderProductID': orderProductID, // ...其他参数 'date': date.toIso8601String() }); print("请求URL: ${Uri.https('************')}"); print("请求Body: $requestBody"); var response = await http.post( Uri.https('************'), body: requestBody, headers: { "Accept": "application/json", "content-type": "application/json" }); print("响应状态码: ${response.statusCode}"); print("响应内容: ${response.body}"); if (response.statusCode == 200) { return true; } else { throw Exception("请求失败,状态码: ${response.statusCode}"); } }
4. 检查实例与权限问题
- 确认
httpService和httpServices是同一个实例(注意变量名多了个s),避免因实例不同导致URL或配置错误 - 检查.NET Core API使用的数据库账号是否有目标表的写入权限,后端是否存在未提交的事务
内容的提问来源于stack exchange,提问作者RajeshS32
相关产品推荐
相关产品推荐

