【发布时间】:2021-05-17 17:06:25
【问题描述】:
我是 Flutter 的一个非常新鲜的开发者,我正在尝试根据标题过滤结果以生成 json 响应。 我想使用计算方法。这种方法只允许 2 个参数,所以我认为我需要使用映射来获取 response.body 和过滤查询。
这是我的 HttpService :
class HttpService {
static List<Post> _parsePosts(Map map) {
print(map);
return map["body"].map((json) => Post.fromJson(json)).where((post) {
final titleLower = post.title.toLowerCase();
final queryLower = map["query"].toLowerCase();
return titleLower.contains(queryLower);
}).toList();
}
static Future<List<Post>> fetchPosts(String query) async {
final uri = Uri.parse('https://jsonplaceholder.typicode.com/posts');
final response = await retry(
() => http.get(uri).timeout(Duration(seconds: 10)),
retryIf: (e) => e is SocketException || e is TimeoutException,
);
Map posts = new Map();
posts["body"] = json.decode(response.body);
posts["query"] = query;
if (response.statusCode == 200) {
return compute(_parsePosts, posts);
} else {
throw Exception("Failed to load posts ${response.statusCode}");
}
}
}
打印(地图);包含带有值的地图 -> https://prnt.sc/131ldey
但问题报告在:return map["body"].map((json) => Post.fromJson(json)).where((post) {
与:_TypeError (type '(dynamic) => dynamic' is not a subtype of type '(dynamic) => bool' of 'test')
我真的不明白这个错误的原因是什么..
【问题讨论】: