我得到了例外
类型‘(动态) => RecentNews’不是“transform”的“(String,dynamic) => MapEntry”类型的子类型。
下面是从JSON转换到JSON的代码
// To parse this JSON data, do
//
// final recentNews = recentNewsFromJson(jsonString);
import 'dart:convert';
List<RecentNews> recentNewsFromJson(String str) => List<RecentNews>.from(json.decode(str).map((x) => RecentNews.fromJson(x)));
String recentNewsToJson(List<RecentNews> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class RecentNews {
RecentNews({
this.id,
this.newsTitle,
this.categoryName,
this.newsDesc,
this.imageUrl,
this.date,
});
String id;
String newsTitle;
String categoryName;
String newsDesc;
String imageUrl;
String date;
factory RecentNews.fromJson(Map<String, dynamic> json) => RecentNews(
id: json["id"].toString(),
newsTitle: json["news_title"],
categoryName: json["category_name"],
newsDesc: json["news_desc"],
imageUrl: json["image_url"],
date: json["date"],
);
Map<String, dynamic> toJson() => {
"id": id,
"news_title": newsTitle,
"category_name": categoryName,
"news_desc": newsDesc,
"image_url": imageUrl,
"date": date,
};
}下面是API方法定义的代码
static Future<List<RecentNews>> getCategoryNews(String id) async
{
String url=Constants.LOCAL_BASE_URL+"api/NewsByCategory/"+id;
try
{
print("checkurl:: "+url);
final response=await http.get(url);
final List<RecentNews> article=recentNewsFromJson(response.body);
return article;
}
catch(e)
{
print("checkurl ======:: "+e.toString());
return List<RecentNews>.empty(growable: true);
}
}下面是API调用的代码
ApiServices.getCategoryNews(this.id).then((categorynews)
{
print(categorynews.length);
setState(() {
_categorynews=categorynews;
_loading=false;
});
});下面是我试图解析的JSON内容
{"current_page":1,"data":[{"id":50,"news_title":"To test","category_name":"Other","news_desc":"
ddd<\/p>","image_url":"public\/media\/news\/260121_09_54_23.jpg","date":"2021-01-26"},{"id":9,"news_title":"Madhya Pradesh 6-Year-Old Raped, Eyes Gouged Out","category_name":"Other","news_desc":"
Damoh:<\/strong><\/p>
A six-year-old girl was raped and her eyes gouged out by the attacker near her home in Damoh in Madhya Pradesh, the police said today. The child has been admitted to hospital in a critical state.<\/p>
She was playing with her friends close to her home last evening when she was drawn away by an unknown man, the police say. She had been missing since then. She was found this morning.<\/p>
\"She was raped and has severe injuries to her eyes,\" said senior police officer Hemant Singh Chauhan.<\/p>
\"We have questioned many suspects and we are working on some leads,\" he told reporters.<\/p>
A police team has gone with the girl and her family to Jabalpur, where she is being treated for severe injuries all over her body.<\/p>
At a time when coronavirus has gripped the world and the country is in lockdown to fight its spread, the savage assault has stunned Madhya Pradesh.<\/p>","image_url":"public\/media\/news\/230420_11_32_51.webp","date":"2020-04-23"}],"first_page_url":"https:\/\/vasithwam.in\/newsapi\/tn-in\/erodedt\/api\/NewsByCategory\/11?page=1","from":1,"last_page":1,"last_page_url":"https:\/\/vasithwam.in\/newsapi\/tn-in\/erodedt\/api\/NewsByCategory\/11?page=1","next_page_url":null,"path":"https:\/\/vasithwam.in\/newsapi\/tn-in\/erodedt\/api\/NewsByCategory\/11","per_page":20,"prev_page_url":null,"to":2,"total":2}发布于 2021-02-07 17:17:52
请更改API ->打开API控制器类&更改此函数。
public function NewsByCategory($id)
{
$data=DB::table('news')
->join('category','category.id','news.category_id')
->where('news.category_id',$id)
->where('news.status',1)
->orderBy('news.id','DESC')
->select('news.id','news.news_title','category.category_name','news.news_desc','news.image_url','news.date')
->get();
return json_encode($data);
}发布于 2021-01-26 18:06:50
我发现,当从json解码时,显式地显示是有帮助的。
您可以在调用Map<String, dynamic>.from(...)时尝试添加RecentNews.fromJson(...)
就像这样:
List<RecentNews> recentNewsFromJson(String str) => List<RecentNews>.from(json.decode(str).map((x) => RecentNews.fromJson(Map<String, dynamic>.from(x))));https://stackoverflow.com/questions/65906223
复制相似问题