I have a question about json in flutter. How can I make an api request.
there is an approximate data that is in the template. but I need to take it through url. I tried many examples that are here, but I still haven't figured it out.
Posts are taken for both Russian and English.
class DummyPosts{
static final Map<String, List<Post>> posts = {
'ru': _ru,
"en": _en,
};
static final List<Post> _ru = [
Post(
id: 1,
title: 'Apple expands renewable energy footprint in Europe',
thumbnail:
'https://www.apple.com/newsroom/images/values/environment/apple_eu-renewable-energy-expansion_wind-farm_09012020_big.jpg.large_2x.jpg',
excerpt: 'World’s largest onshore wind turbines in Denmark and new clean energy efforts in Germany advance Apple’s new 2030 carbon neutral goal',
category: 'Tech',
views: 456,
comments: 23,
htmlContent: '<p>Today Apple announced it will invest in the construction of two of the world’s largest onshore wind turbines, a source of clean, renewable energy that will bring its supply chain and products one step closer to carbon neutrality. Located near the Danish town of Esbjerg, the 200-meter-tall turbines are expected to produce 62 gigawatt hours each year — enough to power almost 20,000 homes — and will act as a test site for powerful offshore wind turbines. The power produced at Esbjerg will support Apple’s data center in Viborg, with all surplus energy going into the Danish grid.</p>',
tags: [
Tag(id: 1, name: 'Post'),
Tag(id: 2, name: 'News'),
Tag(id: 3, name: 'Tech'),
Tag(id: 4, name: 'Apple'),
],
createdAt: DateTime.now().subtract(Duration(days: 900)),
),
];
static final List<Post> _en = [
Post(
id: 1,
title: 'Apple expands renewable energy footprint in Europe',
thumbnail:
'https://www.apple.com/newsroom/images/values/environment/apple_eu-renewable-energy-expansion_wind-farm_09012020_big.jpg.large_2x.jpg',
excerpt: 'World’s largest onshore wind turbines in Denmark and new clean energy efforts in Germany advance Apple’s new 2030 carbon neutral goal',
category: 'Tech',
views: 456,
comments: 23,
htmlContent: '<p>Today Apple announced it will invest in the construction of two of the world’s largest onshore wind turbines, a source of clean, renewable energy that will bring its supply chain and products one step closer to carbon neutrality. Located near the Danish town of Esbjerg, the 200-meter-tall turbines are expected to produce 62 gigawatt hours each year — enough to power almost 20,000 homes — and will act as a test site for powerful offshore wind turbines. The power produced at Esbjerg will support Apple’s data center in Viborg, with all surplus energy going into the Danish grid.</p>',
tags: [
Tag(id: 1, name: 'Post'),
Tag(id: 2, name: 'News'),
Tag(id: 3, name: 'Tech'),
Tag(id: 4, name: 'Apple'),
],
createdAt: DateTime.now().subtract(Duration(days: 900)),
),
];
Model
class Post{
int id;
String title;
String excerpt;
String thumbnail;
String category;
int views;
int comments;
String htmlContent;
List<Tag> tags;
DateTime createdAt;
Post({
this.id,
this.title,
this.excerpt,
this.thumbnail,
this.category,
this.views,
this.comments,
this.htmlContent,
this.tags,
this.createdAt,
});
}
Sorry for my English.