Created with <3 with dartpad.dev.
Created
September 21, 2022 10:52
-
-
Save yousefmasry4/6cf7b908d38f3d7b84d7ac60cf7cf385 to your computer and use it in GitHub Desktop.
Counter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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