1

Is there a way to add https extension hidden by CDN inside mapping in flutter? As you can see in the picture below I'd need to add as link inside my model JSON but the https is hidden by the CDN. How can I solve this issue in order to show up my image from that link? Thank you in advance.

enter image description here

Fetch page for article

class ApiNewsPage {
  String baseUrl = "https://www.assofacile.it/wp-json/wp/v2/posts?_embed";

  Future<List> getNewsArticles() async {
    try {
      var response = await http.get(Uri.parse(baseUrl));
      //print(response);
      if (response.statusCode == 200) {
        return jsonDecode(response.body);
      } else {
        return Future.error("Impossibile ricevere i dati, prova a controllare la connessione");
      }
      // ignore: non_constant_identifier_names
    } catch (SocketException) {
      return Future.error("Impossibile caricare gli articoli");
    }
  }
}

Model page for article

class Article{
  final String? urlImage;
  final String? title;
  final String? description;

  Article({
    required this.urlImage,
    required this.title,
    required this.description,
  });

  factory Article.fromJson(Map<String, dynamic> json) => Article(
    urlImage: json["_embedded"]["wp:featuredmedia"][0]["link"],
    title: json["title"]["rendered"],
    description: json['content']['rendered'],
  );

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};

    data['urlImage'] = urlImage;
    data['title'] = title;
    data['desciption'] = description;
    
    return data;
  }
}

Article content UI

SizedBox(
    height: 190,
    width: double.infinity,
    child: CachedNetworkImage(
      imageUrl: snapshot.data![i]["_embedded"]["wp:featuredmedia"][0]["link"],
                  fit: BoxFit.cover,
                  placeholder: (context, url) => Image.asset("assets/gif/shimmer.gif",
                  width: double.infinity,
                  height: 190,
                  fit: BoxFit.cover,
             ),
           errorWidget: (context, url, error) =>
      Image.asset("assets/images/unloadedImage.png", width: 250, height: 250),
   ),
),
Pimpi Rimpà
  • 75
  • 1
  • 8

1 Answers1

2

You can create a simple method like bellow and verify before using the url that you need

 String checkIfUrlContainPrefixHttp(String url) {
    if (url.startsWith('http://') || url.startsWith('https://')) {
      return url;
    } else {
      return 'http://' + url;
    }
  }
Will
  • 445
  • 2
  • 12
  • Thanks William, I have a doubt, where do I need to add it exactly? I mean in Fetch page for article, in the Model page for article? – Pimpi Rimpà Oct 12 '22 at 15:06
  • That's actually is more up to you, you can do it while parsing the json or directly on the widget creation, for example: imagemUrl: checkIfUrlContainPrefixHttp(URL) – Will Oct 12 '22 at 23:09
  • Sorry William, I am new in Flutter, can you make an example in my code? I still don't get how to integrate this method... – Pimpi Rimpà Oct 13 '22 at 11:07