【问题标题】:type '(dynamic) => RecentNews' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'类型 '(dynamic) => RecentNews' 不是 'transform' 的类型 '(String, dynamic) => MapEntry<dynamic, dynamic>' 的子类型
【发布时间】:2021-04-30 12:35:45
【问题描述】:

我得到了异常
类型 '(dynamic) => RecentNews' 不是 'transform' 的类型 '(String, dynamic) => MapEntry' 的子类型

以下是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}

【问题讨论】:

    标签: json api flutter dart


    【解决方案1】:

    我发现在从 json 解码时,显式化会有所帮助。

    您可以尝试在拨打RecentNews.fromJson(...)时添加Map&lt;String, dynamic&gt;.from(...)

    像这样:

    List<RecentNews> recentNewsFromJson(String str) => List<RecentNews>.from(json.decode(str).map((x) => RecentNews.fromJson(Map<String, dynamic>.from(x))));
    

    【讨论】:

      【解决方案2】:

      请更改 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-22
        • 1970-01-01
        • 2022-11-13
        • 2021-03-16
        • 2018-12-14
        • 2019-04-03
        • 1970-01-01
        • 2020-05-19
        • 2021-08-05
        相关资源
        最近更新 更多