Files
Sanskar Tiwari dc589966b5 Initial commit
2020-03-19 10:49:27 +05:30

84 lines
2.0 KiB
Dart

import 'package:http/http.dart' as http;
import 'package:news_app_api/models/article.dart';
import 'dart:convert';
import 'package:news_app_api/secret.dart';
class News {
List<Article> news = [];
Future<void> getNews() async{
String url = "http://newsapi.org/v2/top-headlines?country=in&excludeDomains=stackoverflow.com&sortBy=publishedAt&language=en&apiKey=${apiKey}";
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if(jsonData['status'] == "ok"){
jsonData["articles"].forEach((element){
if(element['urlToImage'] != null && element['description'] != null){
Article article = Article(
title: element['title'],
author: element['author'],
description: element['description'],
urlToImage: element['urlToImage'],
publshedAt: DateTime.parse(element['publishedAt']),
content: element["content"],
articleUrl: element["url"],
);
news.add(article);
}
});
}
}
}
class NewsForCategorie {
List<Article> news = [];
Future<void> getNewsForCategory(String category) async{
/*String url = "http://newsapi.org/v2/everything?q=$category&apiKey=${apiKey}";*/
String url = "http://newsapi.org/v2/top-headlines?country=in&category=$category&apiKey=${apiKey}";
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if(jsonData['status'] == "ok"){
jsonData["articles"].forEach((element){
if(element['urlToImage'] != null && element['description'] != null){
Article article = Article(
title: element['title'],
author: element['author'],
description: element['description'],
urlToImage: element['urlToImage'],
publshedAt: DateTime.parse(element['publishedAt']),
content: element["content"],
articleUrl: element["url"],
);
news.add(article);
}
});
}
}
}