I getting the exception
type '(dynamic) => RecentNews' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
The following is the code for converting from and to 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,
};
}
The following is the code for the API method definition
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);
}
}
The following is the code for API call
ApiServices.getCategoryNews(this.id).then((categorynews)
{
print(categorynews.length);
setState(() {
_categorynews=categorynews;
_loading=false;
});
});
The following is the JSON content which I am trying to parse
{"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}
question from:
https://stackoverflow.com/questions/65906223/type-dynamic-recentnews-is-not-a-subtype-of-type-string-dynamic-ma