Flutter 颤振从api类型获取数据';字符串';不是类型为';地图<;字符串,动态>';
这是我的测试代码 尝试搜索但不工作。。 这将是我错过的。。 请帮我解决这个问题Flutter 颤振从api类型获取数据';字符串';不是类型为';地图<;字符串,动态>';,flutter,dart,Flutter,Dart,这是我的测试代码 尝试搜索但不工作。。 这将是我错过的。。 请帮我解决这个问题 test('http test', () async { var uri = Uri.parse( 'https:....posts.json'); var response = await http.get(uri); expect(response.statusCode, 200); NewsInfo newsInfo =
test('http test', () async {
var uri = Uri.parse(
'https:....posts.json');
var response = await http.get(uri);
expect(response.statusCode, 200);
NewsInfo newsInfo = NewsInfo.fromJson(jsonDecode(json.encode(response.body)));
expect(newsInfo.id, "dc523f0ed25c");
});
这个id是json到dart的
class NewsInfo {
String id;
int imageId;
int imageThumbId;
Metadata metadata;
List<Paragraphs> paragraphs;
Publication publication;
String subtitle;
String title;
String url;
NewsInfo(
{this.id,
this.imageId,
this.imageThumbId,
this.metadata,
this.paragraphs,
this.publication,
this.subtitle,
this.title,
this.url});
NewsInfo.fromJson(Map<String, dynamic> json) {
id = json['id'];
imageId = json['imageId'];
imageThumbId = json['imageThumbId'];
metadata = json['metadata'] != null
? new Metadata.fromJson(json['metadata'])
: null;
if (json['paragraphs'] != null) {
paragraphs = new List<Paragraphs>();
json['paragraphs'].forEach((v) {
paragraphs.add(new Paragraphs.fromJson(v));
});
}
publication = json['publication'] != null
? new Publication.fromJson(json['publication'])
: null;
subtitle = json['subtitle'];
title = json['title'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
..
当我运行测试时,它显示
test/widget_test.dart 23:47 main。
类型“String”不是类型“Map”的子类型
尝试搜索但不工作。。
这将是我错过的。。
请帮我解决这个问题。您的json响应看起来像一个列表。试一试
test('http test', () async {
var uri = Uri.parse(
'https:....posts.json');
var response = await http.get(uri);
expect(response.statusCode, 200);
NewsInfo newsInfo = NewsInfo.fromJson(jsonDecode(json.encode(response.body)));
expect(newsInfo.id, "dc523f0ed25c");
});
List<NewsInfo> news = json.decode(response.body).map((el) => NewsInfo.fromJson(el)).toList();
List news=json.decode(response.body).map((el)=>NewsInfo.fromJson(el)).toList();