我正在尝试从链接中获取数据。这是链接:-"https://wrestlingworld.co/wp-json/wp/v2/posts?categories=22"
在获取时我得到了错误。我尝试了一些不同的方法来做到这一点,但找不到准确的解决方案。我正在附加我的代码,我尝试了我所做的来解决这个问题。
这是我的控制器:
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());
}
},
),
当试图获取我得到这个错误:类型'_InternalLinkedHashMap
有没有办法解决这个问题?
我试图在不使用Future的情况下编写函数,它根本不起作用。所以我想使用相同的函数获取。
您在从map构造数据时犯了一些错误。该站点有助于查看超文本传输协议请求的完整响应。
在您的情况下,以下数据类应该会有所帮助:
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());
}
},
),