未处理的异常:类型 'String' 不是类型 'List<dynamic> 的子类型
Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>
我之前使用 AWS Lambda 和 API 网关创建了一个 API,它从我的 DynamoDB table.
获取数据
这是我的 lambda 函数
const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async event => {
const params = {
TableName: "//tableName"
};
try {
// Utilising the scan method to get all items in the table
const data = await documentClient.scan(params).promise();
const response = {
statusCode: 200,
body: JSON.stringify(data.Items)
};
return response;
} catch (e) {
return {
statusCode: 500
};
}
};
这是来自 API 的示例响应。
{"statusCode":200,"body":"[{\"product\":\"clear\",\"__typename\":\"ProductModel\",\"size\":\"small\",\"_lastChangedAt\":1625829002606,\"_version\":1,\"company\":\"add\",\"updatedAt\":\"2021-07-09T11:10:02.578Z\",\"category\":\"Pot\",\"createdAt\":\"2021-07-09T11:10:02.578Z\",\"price\":\"69\",\"description\":\"this doesn't even make sense\",\"id\":\"f4acb29c-f8f3-4765-abf6-aef0002a837c\"},\{\"product\":\"product\",\"__typename\":\"ProductModel\",\"size\":\"1\",\"_lastChangedAt\":1625988107746,\"_version\":1,\"company\":\"company\",\"updatedAt\":\"2021-07-11T07:21:47.704Z\",\"category\":\"Climbers\",\"createdAt\":\"2021-07-11T07:21:47.704Z\",\"price\":\"221\",\"description\":\"description\",\"id\":\"0a51d3c3-4df3-4549-abaa-f8b88e8ac407\"}]"}
{"mode":"full","isActive":false}
现在,当我完成解码响应正文的常规步骤时。我得到这个错误
未处理的异常:类型 'String' 不是类型“列表”的子类型。
这是我的做法
void getProductData() async {
List list ;
var response = await http.get(Uri.parse('//api-endpoint'));
Map<String,dynamic> map = json.decode(response.body);
list = map["body"];
}
解码JSON响应
{statusCode: 200, body: [{"product":"clear","__typename":"ProductModel","size":"small","_lastChangedAt":1625829002606,"_version":1,"company":"add","updatedAt":"2021-07-09T11:10:02.578Z","category":"Pot","createdAt":"2021-07-09T11:10:02.578Z","price":"69","description":"this doesn't even make sense","id":"f4acb29c-f8f3-4765-abf6-aef0002a837c"},{"product":"product","__typename":"ProductModel","size":"1","_lastChangedAt":1625988107746,"_version":1,"company":"company","updatedAt":"2021-07-11T07:21:47.704Z","category":"Climbers","createdAt":"2021-07-11T07:21:47.704Z","price":"221","description":"description","id":"0a51d3c3-4df3-4549-abaa-f8b88e8ac407"}]
}
你能试试下面几行吗?
-- 第一个解决方案看起来正确,但我对其进行了一些改进。让我们继续这样。
void getProductData() async {
List<Map<String,dynamic>> list;
final response = await http.get(Uri.parse('//api-endpoint'));
Map<String,dynamic> map = json.decode(response.body);
final body = map['body'];
if(body is List){
list = List.from(map["body"] as List<Map<String,dynamic>>);
}else {
print('Body is\'nt List');
}
}
我之前使用 AWS Lambda 和 API 网关创建了一个 API,它从我的 DynamoDB table.
获取数据这是我的 lambda 函数
const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async event => {
const params = {
TableName: "//tableName"
};
try {
// Utilising the scan method to get all items in the table
const data = await documentClient.scan(params).promise();
const response = {
statusCode: 200,
body: JSON.stringify(data.Items)
};
return response;
} catch (e) {
return {
statusCode: 500
};
}
};
这是来自 API 的示例响应。
{"statusCode":200,"body":"[{\"product\":\"clear\",\"__typename\":\"ProductModel\",\"size\":\"small\",\"_lastChangedAt\":1625829002606,\"_version\":1,\"company\":\"add\",\"updatedAt\":\"2021-07-09T11:10:02.578Z\",\"category\":\"Pot\",\"createdAt\":\"2021-07-09T11:10:02.578Z\",\"price\":\"69\",\"description\":\"this doesn't even make sense\",\"id\":\"f4acb29c-f8f3-4765-abf6-aef0002a837c\"},\{\"product\":\"product\",\"__typename\":\"ProductModel\",\"size\":\"1\",\"_lastChangedAt\":1625988107746,\"_version\":1,\"company\":\"company\",\"updatedAt\":\"2021-07-11T07:21:47.704Z\",\"category\":\"Climbers\",\"createdAt\":\"2021-07-11T07:21:47.704Z\",\"price\":\"221\",\"description\":\"description\",\"id\":\"0a51d3c3-4df3-4549-abaa-f8b88e8ac407\"}]"}
{"mode":"full","isActive":false}
现在,当我完成解码响应正文的常规步骤时。我得到这个错误 未处理的异常:类型 'String' 不是类型“列表”的子类型。 这是我的做法
void getProductData() async {
List list ;
var response = await http.get(Uri.parse('//api-endpoint'));
Map<String,dynamic> map = json.decode(response.body);
list = map["body"];
}
解码JSON响应
{statusCode: 200, body: [{"product":"clear","__typename":"ProductModel","size":"small","_lastChangedAt":1625829002606,"_version":1,"company":"add","updatedAt":"2021-07-09T11:10:02.578Z","category":"Pot","createdAt":"2021-07-09T11:10:02.578Z","price":"69","description":"this doesn't even make sense","id":"f4acb29c-f8f3-4765-abf6-aef0002a837c"},{"product":"product","__typename":"ProductModel","size":"1","_lastChangedAt":1625988107746,"_version":1,"company":"company","updatedAt":"2021-07-11T07:21:47.704Z","category":"Climbers","createdAt":"2021-07-11T07:21:47.704Z","price":"221","description":"description","id":"0a51d3c3-4df3-4549-abaa-f8b88e8ac407"}]
}
你能试试下面几行吗?
-- 第一个解决方案看起来正确,但我对其进行了一些改进。让我们继续这样。
void getProductData() async {
List<Map<String,dynamic>> list;
final response = await http.get(Uri.parse('//api-endpoint'));
Map<String,dynamic> map = json.decode(response.body);
final body = map['body'];
if(body is List){
list = List.from(map["body"] as List<Map<String,dynamic>>);
}else {
print('Body is\'nt List');
}
}