首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型‘(动态) => RecentNews’不是'(String,dynamic) => MapEntry<dynamic,dynamic>‘的子类型。

类型‘(动态) => RecentNews’不是'(String,dynamic) => MapEntry<dynamic,dynamic>‘的子类型。
EN

Stack Overflow用户
提问于 2021-01-26 17:29:31
回答 2查看 1K关注 0票数 1

我得到了例外

类型‘(动态) => RecentNews’不是“transform”的“(String,dynamic) => MapEntry”类型的子类型。

下面是从JSON转换到JSON的代码

代码语言:javascript
复制
// 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方法定义的代码

代码语言:javascript
复制
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调用的代码

代码语言:javascript
复制
  ApiServices.getCategoryNews(this.id).then((categorynews)
    {
      print(categorynews.length);
      setState(() {
        _categorynews=categorynews;
        _loading=false;
      });
    });

下面是我试图解析的JSON内容

代码语言:javascript
复制
{"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}
EN

回答 2

Stack Overflow用户

发布于 2021-02-07 17:17:52

请更改API ->打开API控制器类&更改此函数。

代码语言:javascript
复制
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);
}
票数 1
EN

Stack Overflow用户

发布于 2021-01-26 18:06:50

我发现,当从json解码时,显式地显示是有帮助的。

您可以在调用Map<String, dynamic>.from(...)时尝试添加RecentNews.fromJson(...)

就像这样:

代码语言:javascript
复制
List<RecentNews> recentNewsFromJson(String str) => List<RecentNews>.from(json.decode(str).map((x) => RecentNews.fromJson(Map<String, dynamic>.from(x))));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65906223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档