【问题标题】:How to send list of maps from flutter to webapi如何将地图列表从颤振发送到 webapi
【发布时间】:2020-12-22 08:53:00
【问题描述】:

我想发送类似的数据

[

 {
        "QuizTitle": "QuizList1Title", 
        "QuizDesc": "QuizList1Description",
        "Question": "List1Question1",
        "Choice1": "List1choice1",
        "Choice2": "List1choice2",
        "Choice3": "List1choice3",
        "Choice4": "List1choice4",
        "CorrectAns": "ListcorrectAns1"
    },
    {
        "QuizTitle": "QuizList2Title", 
        "QuizDesc": "QuizList2Description",
        "Question": "List2Question1",
        "Choice1": "List2choice1",
        "Choice2": "List2choice2",
        "Choice3": "List2choice3",
        "Choice4": "List2choice4",
        "CorrectAns": "ListcorrectAns2"
    },
]

从颤振到我的 webapi 有什么线索我怎么能做到这一点?

【问题讨论】:

标签: json flutter webapi


【解决方案1】:

使用以下 POJO 类作为请求。并设置你的参数。

class Autogenerated {
  String quizTitle;
  String quizDesc;
  String question;
  String choice1;
  String choice2;
  String choice3;
  String choice4;
  String correctAns;

  Autogenerated(
      {this.quizTitle,
      this.quizDesc,
      this.question,
      this.choice1,
      this.choice2,
      this.choice3,
      this.choice4,
      this.correctAns});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    quizTitle = json['QuizTitle'];
    quizDesc = json['QuizDesc'];
    question = json['Question'];
    choice1 = json['Choice1'];
    choice2 = json['Choice2'];
    choice3 = json['Choice3'];
    choice4 = json['Choice4'];
    correctAns = json['CorrectAns'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['QuizTitle'] = this.quizTitle;
    data['QuizDesc'] = this.quizDesc;
    data['Question'] = this.question;
    data['Choice1'] = this.choice1;
    data['Choice2'] = this.choice2;
    data['Choice3'] = this.choice3;
    data['Choice4'] = this.choice4;
    data['CorrectAns'] = this.correctAns;
    return data;
  }
}

之后使用json.encode()将对象编码为json并使用dio发送api请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多