【问题标题】:Flutter HTTP post request JSON handling not working properlyFlutter HTTP 发布请求 JSON 处理无法正常工作
【发布时间】:2021-11-14 23:27:07
【问题描述】:

我正在使用带有颤振应用程序的 nodejs 后端。我使用flutter http包发送请求。我想通过请求发送日期时间,所以我将它转换为字符串。因此,在节点后端,这些数据不能转换为原始类型(如 DateTime 或 int)。在flutter和node.js中处理JSON数据的最佳实践是什么?我想通过 http 请求发送数据(日期、字符串、整数)并以正确的格式在后端检索它们。这是颤振功能。这将所有详细信息完全转换为字符串,但我无法在节点后端检索原始格式。

Future<PlanDataInit> createItin(String? name, String? budget,
DateTime? startDate, DateTime? endDate) async {
final response = await http.post(
Uri.parse('http://localhost:3001/itinerary/createitin'),
headers: <String, String>{
  'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
  "Name": name.toString(),
  "startDate": startDate.toString(),
  "endDate": endDate.toString(),
  "initialBud": budget.toString()
}),
);

【问题讨论】:

    标签: node.js json flutter flutter-http jsonencoder


    【解决方案1】:

    使用 startDate.millisecondsSinceEpoch.toString()

    【讨论】:

      【解决方案2】:

      将您的日期转换为 ISOstring 格式。在你的 NodeJS 解析\解码你从 Flutter 发送它们时编码的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 2017-10-21
        • 2020-03-01
        • 2019-05-03
        • 2018-12-05
        相关资源
        最近更新 更多