【发布时间】:2020-08-03 03:27:33
【问题描述】:
Flutter 网络
我从json.decode(res.body) 得到List<dynamic>
并且无法转换为我预期的类型List<Item>
在 chrome 开发工具中,它会抛出
Uncaught (in promise) Error: Type 'List<dynamic>' should be 'List<Item>' to implement expected type 'FutureOr<List<Item>>'.
这是我的代码
import 'package:http/http.dart' as http;
class ApiService {
static Future get({String url, Object params}) async {
final res = await http.get(_root + url);
return json.decode(res.body);
}
/// get 30 items of specific content type
static Future<List<Item>> getContentList({String type, num page = 1}) async {
final List<dynamic> ret = await ApiService.get(
url: _typeMap[type],
);
// If I try `ret.cast<Item>()` it will also throw something like `... type _JsonMap ...`
return ret;
}
}
response body 是黑客新闻列表,
[{comments_count: 466,
domain: "github.com",
id: 22925087,
points: 1162,
time: 1587398440,
time_ago: "8 hours ago",
title: "Shirt Without Stripes",
type: "link",
url: "https://github.com/elsamuko/Shirt-without-Stripes",
user: "elsamuko",
}]
我该如何解决这个问题?
【问题讨论】:
-
请分享您的 json 和您尝试解码 json 的代码。
标签: flutter flutter-web