我在将响应 json 放入列表时遇到问题。
当我尝试转换正文数据(数据来自何处)时,出现错误:
The getter 'body' isn't defined for the type 'Response<dynamic>'.
Try importing the library that defines 'body', correcting the name to the name of an existing getter, or defining a getter or field named 'body'.dartundefined_getter
我的代码是:
onPressed: () async {
Response response;
Dio dio = new Dio();
String url =
'http://192.168.15.5:8090/api/getOs';
response = await dio.post(url, data: {
"numeroos": _numeroOsController.text
});
final extractedData = json.decode(response.body) //Here is the error
as Map<String, dynamic>;
final List<ProdutoOs> loadedProducts = [];
extractedData.forEach((key, value) {
loadedProducts.add(ProdutoOs(
cod_produto: value['Codigo_Produto'],
qtd: value['Qtde'],
desc: value['Descricao'],
numOs: value['Numero_da_OS'],
codOs: value['CodOS']));
Navigator.pop(context, true);
});
}
所以我想把 response.body 放在一个列表中,但我不能,我做错了什么?
我也尝试过使用 response.data 并且没有得到同样的错误,应用程序运行但我得到:
E/flutter (11379): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String'
答案 0 :(得分:0)
也许试试这个:
List responseElements = jsonDecode(response.body);
responseElements.map((singleJsonObject) => {
// do your parsing here
});