Created with <3 with dartpad.dev.
Last active
September 21, 2022 11:20
-
-
Save yousefmasry4/46faef93f274feb09d849319fc90cc90 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 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
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, | |
this.author, | |
required this.title, | |
required this.description, | |
required this.url, | |
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({ | |
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, | |
}; | |
} | |
Future main() async{ | |
var res=await http.get(Uri.parse("https://newsapi.org/v2/everything?q=tesla&from=2022-08-21&sortBy=publishedAt&apiKey=0c341da764424524812d2ecfddaf67bc")); | |
Articles articals=Articles.fromJson(json.decode(res.body)); | |
print(articals.status); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment