I recently started with Flutter and I love how it works. I need to create an App for iOS and Android for the company I work for, so I started with Flutter.
Our website uses WordPress so I am trying to use that as the backend of the App.
It is going Ok and in the App I have a News section, so I have written some code and did a WordPress API call and it works great!
I only have one Issue: the date
and time
format is wrong. We are in Europe so we use dd-mm-yyyy
format and in my App the format is like this: yyyy-mm-dd
and I need to change that but I don’t know how…
My Code:
class Post {
final int id;
final String title;
final String author;
final String excerpt;
final String date;
final String content;
final String image;
bool isSaved = false;
Post(
{
this.content,
this.id,
this.title,
this.excerpt,
this.date,
this.image,
this.author,
}
);
factory Post.fromJSON(Map<String, dynamic> json) {
return Post(
id: json['id'],
title: json['title']['rendered'],
content: json['content']['rendered'],
date: json['date'] != null
? json['date'].toString().replaceFirst('T', ' ')
: null,
image: json['_links']['wp:featuredmedia'] != null
? json['_links']['wp:featuredmedia'][0]['href']
: null,
excerpt: json['excerpt']['rendered'],
author: json['author'].toString()
);
}
}
Can someone please take a look and help me out?
Thanks!
2
Answers
Try this
Fix without using external packages
Instead of
you could write something like
where
Fix using intl package
Instead of
write something like