gpt4 book ai didi

flutter - 类型 '_InternalLinkedHashMap' 不是使用 http 包的类型 'String' 的子类型

转载 作者:行者123 更新时间:2023-12-05 04:21:21 25 4
gpt4 key购买 nike

我正在尝试从链接中获取数据。链接在这里: -“https://wreSTLingworld.co/wp-json/wp/v2/posts?categories=22”

获取时出现错误。我尝试了一些不同的方法来做到这一点,但找不到准确的解决方案。我附上了我的代码,我为解决问题所做的尝试。

这是我的 Controller :

class NewsController{
String url = "https://wrestlingworld.co/wp-json/wp/v2/posts?categories=22";
Future<List> getNews() async {
try{
var response = await http.get(Uri.parse(url));
if(response.statusCode == 200){
print(response.body);
// Map<String, dynamic> resp = json.decode(response.body);
return json.decode(response.body);
// return Map<String, dynamic> json.decode(response.body);
}
else{
return Future.error("Error Handling the request");
}
} catch(Exception){
print(Exception);
return Future.error("Error in the controller");
}
}
}

这是我的模型:

class NewsModel {
late int id;
late String date;
late String status;
late String type;
late String link;
late String title;
late String content;

NewsModel(
{
required this.id,
required this.date,
required this.status,
required this.type,
required this.link,
required this.title,
required this.content});

NewsModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
date = json['date'];
status = json['status'];
type = json['type'];
link = json['link'];
title = json['title'];
content = json['content'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['date'] = this.date;
data['status'] = this.status;
data['type'] = this.type;
data['link'] = this.link;
if (this.title != null) {
data['title'] = this.title;
}
if (this.content != null) {
data['content'] = this.content;
}
return data;
}
}



这里是我要获取的地方:

FutureBuilder(
future: newsController.getNews(),
builder: (context, snapShot) {
if (snapShot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapShot.data?.length,
itemBuilder: (context, item){
return NewsCard(title: snapShot.data?[item]['title']);
})
);
}
else {
return const Center(child: CircularProgressIndicator());
}
},
),

在尝试获取时出现此错误:type '_InternalLinkedHashMap ' is not a subtype of type 'String'

有什么办法可以解决这个问题吗?

我尝试在不使用 Future 的情况下编写函数,但它根本不起作用。所以我想使用相同的函数获取数据。

最佳答案

您在从 map 构造数据时犯了一些错误。 This site有助于查看 http 请求的完整响应。

在您的情况下,以下数据类应该有所帮助:

class YourModel {
final int id;
final String date;
final String status;
final String type;
final String link;
final String title;
final String content;

YourModel({
required this.id,
required this.date,
required this.status,
required this.type,
required this.link,
required this.title,
required this.content,
});

factory YourModel.fromJson(Map<String, dynamic> json) {
return YourModel(
id: json['id'],
date: json['date'],
status: json['status'],
type: json['type'],
link: json['link'],
title: json['title']['rendered'],
content: json['content']['rendered'],
);
}
}

也为您的代码尝试这些:

FutureBuilder(
future: newsController.getNews(),
builder: (context, snapShot) {
if (snapShot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapShot.data?.length,
itemBuilder: (context, item){
return NewsCard(title: snapShot.data?[item]['title']['rendered']);
})
);
}
else {
return const Center(child: CircularProgressIndicator());
}
},
),

关于flutter - 类型 '_InternalLinkedHashMap<String, dynamic>' 不是使用 http 包的类型 'String' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74239902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com