Created
August 14, 2022 13:11
-
-
Save mrasityilmaz/cde94c739c8313ad6ecbd49bb8074a75 to your computer and use it in GitHub Desktop.
Linkten dönen veriyi https://javiercbk.github.io/json_to_dart/ sitesini kullanarak dart için classlarını aldım sonra fonksiyonunu one göre düzenledim.
This file contains hidden or 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
class ArticleModel { | |
String? status; | |
int? totalResults; | |
List<Articles>? articles; | |
ArticleModel({this.status, this.totalResults, this.articles}); | |
ArticleModel.fromJson(Map<String, dynamic> json) { | |
status = json['status']; | |
totalResults = json['totalResults']; | |
if (json['articles'] != null) { | |
articles = <Articles>[]; | |
json['articles'].forEach((v) { | |
articles!.add(Articles.fromJson(v)); | |
}); | |
} | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = <String, dynamic>{}; | |
data['status'] = status; | |
data['totalResults'] = totalResults; | |
if (articles != null) { | |
data['articles'] = articles!.map((v) => v.toJson()).toList(); | |
} | |
return data; | |
} | |
} | |
class Articles { | |
Source? source; | |
String? author; | |
String? title; | |
String? description; | |
String? url; | |
String? urlToImage; | |
String? publishedAt; | |
String? content; | |
Articles( | |
{this.source, | |
this.author, | |
this.title, | |
this.description, | |
this.url, | |
this.urlToImage, | |
this.publishedAt, | |
this.content}); | |
Articles.fromJson(Map<String, dynamic> json) { | |
source = json['source'] != null ? Source.fromJson(json['source']) : null; | |
author = json['author']; | |
title = json['title']; | |
description = json['description']; | |
url = json['url']; | |
urlToImage = json['urlToImage']; | |
publishedAt = json['publishedAt']; | |
content = json['content']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = <String, dynamic>{}; | |
if (source != null) { | |
data['source'] = source!.toJson(); | |
} | |
data['author'] = author; | |
data['title'] = title; | |
data['description'] = description; | |
data['url'] = url; | |
data['urlToImage'] = urlToImage; | |
data['publishedAt'] = publishedAt; | |
data['content'] = content; | |
return data; | |
} | |
} | |
class Source { | |
String? id; | |
String? name; | |
Source({this.id, this.name}); | |
Source.fromJson(Map<String, dynamic> json) { | |
id = json['id']; | |
name = json['name']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = <String, dynamic>{}; | |
data['id'] = id; | |
data['name'] = name; | |
return data; | |
} | |
} |
This file contains hidden or 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
Future<List<Articles>?> getNews() async { | |
final response = await http.get(Uri.parse( | |
"https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=7457ce54364a42eb9e0ecfdaf43c375c")); | |
if (response.statusCode == 200) { | |
var jsonData = jsonDecode(response.body); | |
ArticleModel model = ArticleModel.fromJson(jsonData); | |
if (model.status == "ok") { | |
return model.articles; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment