Skip to content

Instantly share code, notes, and snippets.

@yousefmasry4
Created September 21, 2022 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yousefmasry4/6cf7b908d38f3d7b84d7ac60cf7cf385 to your computer and use it in GitHub Desktop.
Save yousefmasry4/6cf7b908d38f3d7b84d7ac60cf7cf385 to your computer and use it in GitHub Desktop.
Counter example
// To parse this JSON data, do
//
// final articles = articlesFromJson(jsonString);
import 'dart:convert';
Articles articlesFromJson(String str) => Articles.fromJson(json.decode(str));
String articlesToJson(Articles data) => json.encode(data.toJson());
class Articles {
Articles({
required this.status,
required this.totalResults,
required this.articles,
});
String status;
int totalResults;
List<Article> articles;
factory Articles.fromJson(Map<String, dynamic> json) => Articles(
status: json["status"],
totalResults: json["totalResults"],
articles: List<Article>.from(json["articles"].map((x) => Article.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"totalResults": totalResults,
"articles": List<dynamic>.from(articles.map((x) => x.toJson())),
};
}
class Article {
Article({
required this.source,
required this.author,
required this.title,
required this.description,
required this.url,
required this.urlToImage,
required this.publishedAt,
required this.content,
});
Source source;
String author;
String title;
String description;
String url;
String urlToImage;
DateTime publishedAt;
String content;
factory Article.fromJson(Map<String, dynamic> json) => Article(
source: Source.fromJson(json["source"]),
author: json["author"] == null ? null : json["author"],
title: json["title"],
description: json["description"],
url: json["url"],
urlToImage: json["urlToImage"] == null ? null : json["urlToImage"],
publishedAt: DateTime.parse(json["publishedAt"]),
content: json["content"],
);
Map<String, dynamic> toJson() => {
"source": source.toJson(),
"author": author == null ? null : author,
"title": title,
"description": description,
"url": url,
"urlToImage": urlToImage == null ? null : urlToImage,
"publishedAt": publishedAt.toIso8601String(),
"content": content,
};
}
class Source {
Source({
required this.id,
required this.name,
});
String id;
String name;
factory Source.fromJson(Map<String, dynamic> json) => Source(
id: json["id"] == null ? null : json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment