Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

json - type '(dynamic) => RecentNews' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please change API -> Open API Controller class & change this function .

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...