Created with <3 with dartpad.dev.
Created
September 21, 2022 11:09
-
-
Save yousefmasry4/ffdb6836015c864e28bd7bebee805d10 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, | |
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, | |
}; | |
} | |
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")); | |
print(res.statusCode); | |
Articles articals=Articles.fromJson(json.decode(res.body)); | |
print(articals.articles.first.author); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment