-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnews.dart
More file actions
26 lines (24 loc) · 815 Bytes
/
news.dart
File metadata and controls
26 lines (24 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:newsapp/model/article_model.dart';
class News{
Future<List<Article>>getNews(String category)async{
String url ='https://newsapi.org/v2/top-headlines?country=in&apiKey=493cf1d1db874e6595b36b7b17acd4c9';
var response=await http.get(Uri.parse(url));
if(response.statusCode == 200){
Map<String, dynamic> _json = json.decode(response.body);
List<dynamic> body = _json['articles'];
List<Article> articles = [];
body.forEach((element) {
try {
articles.add(Article.fromJson(element));
} catch (_){}
});
return articles;
print(response.body);
}
else
throw
Exception(("Can't get the Articles"));
}
}