【问题标题】:How to pass ArrayList item to body of http request (post) in Flutter?如何将 ArrayList 项传递给 Flutter 中的 http 请求(post)正文?
【发布时间】:2026-02-17 02:00:02
【问题描述】:

我有一个 ArrayList,用于存储来自我的 API 的数据 List<GetTestFeeMap> reponseArray =[]; // Storing API response

然后将数据添加到我的 ArraylistreponseArray.add(getTestFeeObj!);

现在尝试获取每个encTestId。但它只给出第一个值(显然)。 我想传递所有可能在我的列表中的encTestId(到 http 正文)(用逗号分隔。)

 getTestFeeObj=GetTestFeeMap.fromJson(jsonResponse);
              for (var i = 0; i < reponseArray.length; i++) {
                    eNcTestIdInList=(reponseArray[i].encTestId)!;
                    }

然后传递给我的http请求体

  body: ({

        'EncDoctorId'   : eNcTestIdInList,
     )}

邮递员中正确正文的示例

{
"EncDoctorId": "I3uXyzcuDZf21SSe5fHnSQ==,7Ch2aVnhokZtRWyJtuDA/A==",   // 2 encTestId 
}
        

希望你能理解我的问题。

【问题讨论】:

    标签: json flutter api dart


    【解决方案1】:

    使用这个:

    eNcTestIdInList+=(reponseArray[i].encTestId);
    if (i < reponseArray.length -1) eNcTestIdInList+= ",";
    

    【讨论】:

    • 它显示The method 'add' isn't defined for the type 'String'.我在forloop里面这样放`eNcTestIdInList=eNcTestIdInList.add(reponseArray[i].encTestId);`
    • 哦,没有意识到这是一个字符串,现在试试我编辑了我的答案
    • Api 响应失败 {"Status":"0","Message":"Wrong operation","EncBookingId":null} 我相信它是因为它加起来了字符串 (A+b)= AB。 Insted 它应该与comma(,) 分开,如我的正文示例"EncDoctorId": "I3uXyzcuDZf21SSe5fHnSQ==,7Ch2aVnhokZtRWyJtuDA/A==",
    • 我在控制台中打印eNcTestIdInListI3uXyzcuDZf21SSe5fHnSQ==I3uXyzcuDZf21SSe5fHnSQ==7Ch2aVnhokZtRWyJtuDA/A== 它加起来整个字符串。如何用逗号分隔它们?
    最近更新 更多