在Dartlang/Flutter中,我试图使用.map()和.toList()创建一个地图列表,但得到了上面的错误消息。我尝试在不同的地方添加类型注释,但它们只是导致了类似但不同的类型错误。
这是响应正文。
Response body: {"data":{"logsread":[{"id":"7a2dd3b","email":"email@gmail.com"}]}}这是代码。
http.post(url, body: read2).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
var tempTodos;
tempTodos = jsonDecode(response.body)['data']['logsread']
.map((node) => {
'id': 0,
'title': node['email'],
'score': 0,
})
.toList();
return Upd(model.copyWith(todoList: tempTodos));Model类的定义如下:
class Model {
final String todo;
final List<String> todos;
final Map todoWithScore;
final List<Map> todoList;
Model(this.todo, this.todos, this.todoWithScore, this.todoList);
Model copyWith({
String todo,
List<String> todos,
Map todoWithScore,
List<Map> todoList,
}) =>
Model(
todo ?? this.todo,
todos ?? this.todos,
todoWithScore ?? this.todoWithScore,
todoList ?? this.todoList,
);
}发布于 2019-02-13 14:30:16
添加“as List”后再试,如下所示。
tempTodos = (jsonDecode(response.body)['data']['logsread'] as List )
.map((node) => {
'id': 0,
'title': node['email'],
'score': 0,
})
.toList();https://stackoverflow.com/questions/54663796
复制相似问题