如何在Flutter中将json列表传递到http请求的正文(post)?

时间:2019-09-24 16:50:24

标签: json http post flutter dart

我有一些将由用户填写的对象。我将这些对象解析为json并将该json添加到列表中以传递请求正文。但是我做不到。

  incrementListPaymentSlipes(PaymentSlipes objPayment) async {
    objPayment.name = "Douglas";
    objPayment.personalId = "00000000000";
    Map<String, dynamic> json = objPayment.toJson();
    listPaymentSlipes.add(jsonEncode(json));
  }
var response = await http.post(url, body: {
"payment_slips": listPaymentSlipes,
}

正确的身体示例:

"payment_slips": [
    {      
      "personal_id": "01888728680",
      "name": "Fulano da Silva"
    }
  ]
 {"error":"'{{personal_id: 00000000000, name: Douglas}}' é invalido como 'payment_slips'","code":"payment_slips_invalid"}```

2 个答案:

答案 0 :(得分:1)

因此,看来您没有获得所需的JSON。我整理了一些代码,向您展示如何获得所需的身体。

链接在DartPad https://dartpad.dartlang.org/3fde03078e56efe13d31482dea8e5eef中运行

    class PaymentSlipes {
      String name;
      String personaId;

      ObjPayment({this.name, this.personaId});

      //You create this to convert your object to JSON
      Map<String, dynamic> toJson() => {'name': name, 'personaId': personaId};
    }

    // This method is needed to convert the list of ObjPayment into an Json Array
    List encondeToJson(List<PaymentSlipes> list) {
      List jsonList = List();
      list.map((item) => jsonList.add(item.toJson())).toList();
      return jsonList;
    }

    // This is an example and this code will run in DartPad link above
    void main() {
      PaymentSlipes objPayment = PaymentSlipes(name: "Douglas", personaId: "123425465");
      PaymentSlipes objPayment2 = PaymentSlipes(name: "Dave", personaId: "123425465;
      PaymentSlipes objPayment3 = PaymentSlipes(name: "Mike", personaId: "123425465");
      var list = [objPayment, objPayment2, objPayment3];

      // This is the mapping of the list under the key payment_slips as per your example and the body i would pass to the POST
      var finalJson = {"payment_slips": encondeToJson(list)};
      print(finalJson);
    }

答案 1 :(得分:0)

您可以用非常简单的方式来做。创建payment.dart文件并复制粘贴以下代码类。

class PaymentList {
PaymentList(this.payments);

List<Payment> payments;

Map<String, dynamic> toJson() => <String, dynamic>{
    'payment_slips': payments,
  };
}

class Payment {
Payment({this.name, this.personalId});

String name;
String personalId;

Map<String, dynamic> toJson() => <String, dynamic>{
    'personal_id': personalId,
    'name': name,
  };
}

现在,您可以使用以下代码将其转换为所需的json格式。例如,我正在创建一个虚拟列表:

final PaymentList paymentList =
    PaymentList(List<Payment>.generate(2, (int index) {
  return Payment(name: 'Person $index', personalId: '$index');
}));
final String requestBody = json.encoder.convert(paymentList);

requestBody变量将具有j​​son字符串,如下所示:

  {"payment_slips": [
    {
        "personal_id": "0",
        "name": "Person 0"
    },
    {
        "personal_id": "1",
        "name": "Person 1"
    }
  ]}

现在您可以调用api了:

var response = await http.post(url, body: requestBody}

注意:请导入以下软件包,访问json将需要该软件包:

import 'dart:convert';