我正在嘗試從鏈接中獲取資料。這是鏈接:-“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<String, dynamic>”不是“字串”型別的子型別
有沒有辦法解決這個問題?
我試圖在不使用 Future 的情況下撰寫函式,但它根本不起作用。所以我想使用相同的函式來獲取。
uj5u.com熱心網友回復:
您在從地圖構造資料時犯了一些錯誤。該站點有助于查看來自 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());
}
},
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/523016.html
標籤:扑镖http
