mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-05-19 05:05:53 +00:00
* Added a News&Memes-App/Sriraj-dev * Added Screen recording and Screenshots * Update README.md * Added demo directory with sample images/recording * Update README.md
This commit is contained in:
20
news_memes_app/lib/services/getMemes.dart
Normal file
20
news_memes_app/lib/services/getMemes.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
Future<List<dynamic>> showMemes() async {
|
||||
Uri uri = Uri.parse('https://meme-api.herokuapp.com/gimme/10');
|
||||
final response = await http.get(uri);
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
print(response.body);
|
||||
Map<String, dynamic> map = json.decode(response.body);
|
||||
List<dynamic> memes = map['memes'];
|
||||
List<dynamic> memeUrls =
|
||||
memes.map((e) => e['preview'][e['preview'].length - 1]).toList();
|
||||
return memeUrls;
|
||||
} else {
|
||||
print('Error - ${response.statusCode}');
|
||||
return [''];
|
||||
}
|
||||
}
|
||||
42
news_memes_app/lib/services/getNews.dart
Normal file
42
news_memes_app/lib/services/getNews.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final String apikey = 'YOUR_API_KEY';
|
||||
|
||||
Future<List<NewsModel>> showNews() async {
|
||||
Uri uri = Uri.parse(
|
||||
'https://newsapi.org/v2/top-headlines?country=in&apiKey=$apikey');
|
||||
|
||||
final response = await http.get(uri);
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
print(response.body);
|
||||
Map<String, dynamic> result = json.decode(response.body);
|
||||
|
||||
List articles = result['articles'];
|
||||
print('Articles are - $articles');
|
||||
List<NewsModel> news = articles.map((e) => NewsModel.fromjson(e)).toList();
|
||||
|
||||
return news;
|
||||
} else {
|
||||
print('Error - ${response.statusCode}');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
class NewsModel {
|
||||
String title, description, content, imageUrl, date;
|
||||
|
||||
NewsModel(
|
||||
this.title, this.description, this.content, this.imageUrl, this.date);
|
||||
|
||||
factory NewsModel.fromjson(Map<String, dynamic> jsondata) {
|
||||
return NewsModel(
|
||||
jsondata['title'] ?? '',
|
||||
jsondata['description'] ?? '',
|
||||
jsondata['content'] ?? '',
|
||||
jsondata['urlToImage'] ?? 'NoImage',
|
||||
jsondata['publishedAt'] ?? '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user