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

dart:convert的json.encoder字符串转换疑问及Flutter向FastAPI发送PUT请求的序列化问题咨询

dart:convert的json.encoder字符串转换疑问及Flutter向FastAPI发送PUT请求的序列化问题咨询

我现在正尝试用Flutter代码给FastAPI后端发送HTTP PUT请求,发送前会先把数据转成JSON格式再放到请求体里。下面是我的数据类代码:

class ListeningHistory {
  final String userId;
  List<BareMinPost> podcasts;

  ListeningHistory({required this.userId, required this.podcasts});

  Map<String, dynamic> toJson() {
    podcasts = []; // for simplicity's sake
    return {'user_id': userId, 'podcasts': jsonEncode(podcasts)};
  }

请注意:为了示例简单,我特意把实际的podcasts替换成了空数组。

class BareMinPost {
  final String title;
  final String publishedDate;
  final int pausedAt;

  BareMinPost({
    required this.title,
    required this.publishedDate,
    required this.pausedAt,
  });

  BareMinPost.fromJson(Map<String, dynamic> json)
      : title = json['title'] as String,
        publishedDate = json['publishedDate'] as String,
        pausedAt = jso...

备注:内容来源于stack exchange,提问作者newbie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.16 10:34:50