大家好,我一直想找出api的响应,试图获取对象列表并在调试中打印,但是我做不到,我想我缺少了一些东西
我想获取这些单词的列表,但是我不知道该怎么做?
alanin, alaunian, alnein, alnuin, eolienne, laniinae, lanny, lenaean, lenin, lenny, lennow, leonine, lewanna, linen, linene, lineny, linin, linn, linnaea, linne, linon, lionne, loanin, lunn
我正在使用Flutter和http包,也尝试了dio
这是我的代码和响应
Future<List> onBtnPress(String text) async {
http.Response response = await http.post(url, body: body);
return jsonDecode(response.body);
}
List _data = await onBtnPress(textController.text);
debugPrint(_data.toList().toString());
[522, [alanin, alaunian, alnein, alnuin, eolienne, laniinae, lanny, lenaean, lenin, lenny, lennow, leonine, lewanna, linen, linene, lineny, linin, linn, linnaea, linne, linon, lionne, loanin, lunn], 373, [macoma, mecum, myokymia, mokum]]
我尝试了_data[0][index]
,但它只显示5 2 2
答案 0 :(得分:1)
使用this
创建响应模型Future<listResponse> getList() async {
final HttpResponse response = await this.getRequest(ApiType.getObjectList, params: {}, );
responseList objData;
if (response.status == 200 ) {
objData = responseList.fromJson(response.json);
return listResponse(status: response.status, message: response.errMessage, data: objData);
}else{
return listResponse(status: response.status, message: response.errMessage, data: null);
}
}
希望获得帮助!
答案 1 :(得分:0)
您正在使用POST方法而不是Get。这就是为什么要获取状态码的原因。 试试
Future<List> onBtnPress(String text) async {
http.Response response = await http.get(url, body: body);
return jsonDecode(response.body);
如果需要身份验证,请确保将其添加到标题中
答案 2 :(得分:0)
我想通了,谢谢 我得到的回应是一个列表
类似于list [0] = 522; list [1] = [asdf,adsf,asdf,]
非常感谢您
data[0] = 522
&
data[1] = [alanin, alaunian, alnein, alnuin, eolienne, laniinae, lanny, lenaean, lenin, lenny, lennow, leonine, lewanna, linen, linene, lineny, linin, linn, linnaea, linne, linon, lionne, loanin, lunn]
答案 3 :(得分:-1)
var rawJson = '{"url":"http://blah.jpg","id":1}';
Map<String,dynamic> parsedJson = json.decode(rawJson);
var imageModel = ImageModel(id: parsedJson['id'],url: parsedJson['url']);
class ImageModel
{ int id; String url; ImageModel({this.id,this.url});}