0

I am trying to get the data from my json feed which is this Json

But when I try to use the body result it is not working ( but it works with other jsons like this one https://jsonplaceholder.typicode.com/users), my code looks like this:

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'noticias.dart';

class Services {
static const String url =
  'https://studiofutbol.com.ec/api/json/feed_master.php?pagina=';

static Future<Portada> getNoticias() async {
try {
  var headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Requested-With': 'XMLHttpRequest',
  };
  final response = await http.get(url, headers: headers);
  debugPrint(response.body);
  if (200 == response.statusCode) {
    final Portada noticiasportada = portadaFromJson(response.body);
    return noticiasportada;
  } else {
    return Portada();
  }
} catch (e) {
  return Portada();
}
  }
}

I added those headers but it did not help, what could be causing the issue?

2
  • Can you add error or response to the question? Commented Jan 27, 2021 at 6:50
  • Please add debugPrint(response.body); what you are getting here Commented Jan 27, 2021 at 7:13

1 Answer 1

1

It work for me like this

Future<Portada> getNoticias() async {
    try {
      HttpClient httpClient = new HttpClient();
      HttpClientRequest request = await httpClient.getUrl(Uri.parse(
          "https://studiofutbol.com.ec/api/json/feed_master.php?pagina="));
      request.headers.set('content-type', 'application/json');
     
      HttpClientResponse response = await request.close();
      String reply = await response.transform(utf8.decoder).join();
      print(reply);
      httpClient.close();
      Map<String, dynamic> map = json.decode(reply);
      final Portada noticiasportada = Portada.fromMap(map);
      return noticiasportada;
    } catch (e) {
      return Portada();
    }
  }

Model Class

import 'dart:convert';

Portada portadaFromMap(String str) => Portada.fromMap(json.decode(str));

String portadaToMap(Portada data) => json.encode(data.toMap());

class Portada {
  Portada({
    this.json,
    this.menu,
    this.banners,
    this.tags,
    this.equipos,
    this.menuIos,
    this.version,
    this.versionIos,
  });

  List<Json> json;
  List<Menu> menu;
  Banners banners;
  List<Tag> tags;
  List<Equipo> equipos;
  List<MenuIo> menuIos;
  String version;
  String versionIos;

  factory Portada.fromMap(Map<String, dynamic> json) => Portada(
    json: json["Json"] == null ? null : List<Json>.from(json["Json"].map((x) => Json.fromMap(x))),
    menu: json["menu"] == null ? null : List<Menu>.from(json["menu"].map((x) => Menu.fromMap(x))),
    banners: json["banners"] == null ? null : Banners.fromMap(json["banners"]),
    tags: json["tags"] == null ? null : List<Tag>.from(json["tags"].map((x) => Tag.fromMap(x))),
    equipos: json["equipos"] == null ? null : List<Equipo>.from(json["equipos"].map((x) => Equipo.fromMap(x))),
    menuIos: json["menu_ios"] == null ? null : List<MenuIo>.from(json["menu_ios"].map((x) => MenuIo.fromMap(x))),
    version: json["version"] == null ? null : json["version"],
    versionIos: json["version_ios"] == null ? null : json["version_ios"],
  );

  Map<String, dynamic> toMap() => {
    "Json": json == null ? null : List<dynamic>.from(json.map((x) => x.toMap())),
    "menu": menu == null ? null : List<dynamic>.from(menu.map((x) => x.toMap())),
    "banners": banners == null ? null : banners.toMap(),
    "tags": tags == null ? null : List<dynamic>.from(tags.map((x) => x.toMap())),
    "equipos": equipos == null ? null : List<dynamic>.from(equipos.map((x) => x.toMap())),
    "menu_ios": menuIos == null ? null : List<dynamic>.from(menuIos.map((x) => x.toMap())),
    "version": version == null ? null : version,
    "version_ios": versionIos == null ? null : versionIos,
  };
}

class Banners {
  Banners({
    this.splash,
    this.splashiphone,
    this.main,
    this.interna,
  });

  Interna splash;
  Interna splashiphone;
  Interna main;
  Interna interna;

  factory Banners.fromMap(Map<String, dynamic> json) => Banners(
    splash: json["splash"] == null ? null : Interna.fromMap(json["splash"]),
    splashiphone: json["splashiphone"] == null ? null : Interna.fromMap(json["splashiphone"]),
    main: json["main"] == null ? null : Interna.fromMap(json["main"]),
    interna: json["interna"] == null ? null : Interna.fromMap(json["interna"]),
  );

  Map<String, dynamic> toMap() => {
    "splash": splash == null ? null : splash.toMap(),
    "splashiphone": splashiphone == null ? null : splashiphone.toMap(),
    "main": main == null ? null : main.toMap(),
    "interna": interna == null ? null : interna.toMap(),
  };
}

class Interna {
  Interna({
    this.imagen,
    this.link,
  });

  String imagen;
  String link;

  factory Interna.fromMap(Map<String, dynamic> json) => Interna(
    imagen: json["imagen"] == null ? null : json["imagen"],
    link: json["link"] == null ? null : json["link"],
  );

  Map<String, dynamic> toMap() => {
    "imagen": imagen == null ? null : imagen,
    "link": link == null ? null : link,
  };
}

class Equipo {
  Equipo({
    this.id,
    this.nombre,
  });

  int id;
  String nombre;

  factory Equipo.fromMap(Map<String, dynamic> json) => Equipo(
    id: json["id"] == null ? null : json["id"],
    nombre: json["nombre"] == null ? null : json["nombre"],
  );

  Map<String, dynamic> toMap() => {
    "id": id == null ? null : id,
    "nombre": nombre == null ? null : nombre,
  };
}

class Json {
  Json({
    this.id,
    this.title,
    this.titleint,
    this.permalink,
    this.content,
    this.excerpt,
    this.date,
    this.author,
    this.image,
    this.fuente,
    this.thumbnail,
    this.categories,
    this.tags,
    this.contentIphone,
    this.videoEnlace,
  });

  int id;
  String title;
  String titleint;
  String permalink;
  String content;
  String excerpt;
  DateTime date;
  String author;
  List<dynamic> image;
  String fuente;
  String thumbnail;
  List<String> categories;
  List<String> tags;
  String contentIphone;
  String videoEnlace;

  factory Json.fromMap(Map<String, dynamic> json) => Json(
    id: json["id"] == null ? null : json["id"],
    title: json["title"] == null ? null : json["title"],
    titleint: json["titleint"] == null ? null : json["titleint"],
    permalink: json["permalink"] == null ? null : json["permalink"],
    content: json["content"] == null ? null : json["content"],
    excerpt: json["excerpt"] == null ? null : json["excerpt"],
    date: json["date"] == null ? null : DateTime.parse(json["date"]),
    author: json["author"] == null ? null : json["author"],
    image: json["image"] == null ? null : List<dynamic>.from(json["image"].map((x) => x)),
    fuente: json["fuente"] == null ? null : json["fuente"],
    thumbnail: json["thumbnail"] == null ? null : json["thumbnail"],
    categories: json["categories"] == null ? null : List<String>.from(json["categories"].map((x) => x)),
    tags: json["tags"] == null ? null : List<String>.from(json["tags"].map((x) => x)),
    contentIphone: json["contentIphone"] == null ? null : json["contentIphone"],
    videoEnlace: json["video_enlace"] == null ? null : json["video_enlace"],
  );

  Map<String, dynamic> toMap() => {
    "id": id == null ? null : id,
    "title": title == null ? null : title,
    "titleint": titleint == null ? null : titleint,
    "permalink": permalink == null ? null : permalink,
    "content": content == null ? null : content,
    "excerpt": excerpt == null ? null : excerpt,
    "date": date == null ? null : date.toIso8601String(),
    "author": author == null ? null : author,
    "image": image == null ? null : List<dynamic>.from(image.map((x) => x)),
    "fuente": fuente == null ? null : fuente,
    "thumbnail": thumbnail == null ? null : thumbnail,
    "categories": categories == null ? null : List<dynamic>.from(categories.map((x) => x)),
    "tags": tags == null ? null : List<dynamic>.from(tags.map((x) => x)),
    "contentIphone": contentIphone == null ? null : contentIphone,
    "video_enlace": videoEnlace == null ? null : videoEnlace,
  };
}

class Menu {
  Menu({
    this.url,
    this.nombre,
  });

  String url;
  String nombre;

  factory Menu.fromMap(Map<String, dynamic> json) => Menu(
    url: json["url"] == null ? null : json["url"],
    nombre: json["nombre"] == null ? null : json["nombre"],
  );

  Map<String, dynamic> toMap() => {
    "url": url == null ? null : url,
    "nombre": nombre == null ? null : nombre,
  };
}

class MenuIo {
  MenuIo({
    this.nombre,
    this.url,
    this.segue,
    this.img,
  });

  String nombre;
  String url;
  String segue;
  String img;

  factory MenuIo.fromMap(Map<String, dynamic> json) => MenuIo(
    nombre: json["nombre"] == null ? null : json["nombre"],
    url: json["url"] == null ? null : json["url"],
    segue: json["segue"] == null ? null : json["segue"],
    img: json["img"] == null ? null : json["img"],
  );

  Map<String, dynamic> toMap() => {
    "nombre": nombre == null ? null : nombre,
    "url": url == null ? null : url,
    "segue": segue == null ? null : segue,
    "img": img == null ? null : img,
  };
}

class Tag {
  Tag({
    this.id,
    this.nombre,
    this.url,
  });

  int id;
  String nombre;
  String url;

  factory Tag.fromMap(Map<String, dynamic> json) => Tag(
    id: json["id"] == null ? null : json["id"],
    nombre: json["nombre"] == null ? null : json["nombre"],
    url: json["url"] == null ? null : json["url"],
  );

  Map<String, dynamic> toMap() => {
    "id": id == null ? null : id,
    "nombre": nombre == null ? null : nombre,
    "url": url == null ? null : url,
  

 };
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, your model class looks so neat and clean, Can you please tell me by which tool you have generated it ?
This worked thanks, but what would be the problem with the method I am using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.