![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-11.51.36.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-11.54.48.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-11.57.45-1024x766.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-12.00.54.png)
import 'package:flutter/material.dart';
class NewsPages extends StatefulWidget {
const NewsPages({Key? key}) : super(key: key);
@override
_NewsPagesState createState() => _NewsPagesState();
}
class _NewsPagesState extends State<NewsPages> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News'),
),
);
}
}
main.dart
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-16.53.31-1024x886.png)
import 'package:flutter/material.dart';
import 'package:flutter_news/pages/NewsPages.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: NewsPages(),
);
}
}
![](https://thana.in.th/wp-content/uploads/2021/06/Simulator-Screen-Shot-iPhone-12-Pro-2021-06-23-at-16.51.51-473x1024.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-17.04.39.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-17.02.19.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-17.06.35-1.png)
Posman
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-17.11.34-1024x717.png)
pubspec.yaml
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-22.59.07.png)
http: ^0.13.3
intl: ^0.17.0
NewsPage.dart
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-23.08.08.png)
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
...
class _NewsPagesState extends State<NewsPages> {
List<dynamic> articles = [];
int totalResults = 0;
bool isLoading = true;
_getData()
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-23.14.23-1024x624.png)
_getData()async {
try {
var url = Uri.parse(
'https://newsapi.org/v2/everything?q=apple&from=2021-06-22&to=2021-06-22&sortBy=popularity&apiKey=xxx');
var response = await http.get(url);
final Map<String, dynamic> news = convert.jsonDecode(response.body);
setState(() {
articles = news['articles'];
totalResults = news['totalResults'];
isLoading = false;
});
} catch(e) {
setState(() {
isLoading = false;
});
}
}
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-23.15.42.png)
@override
void initState() {
// TODO: implement initState
super.initState();
_getData();
}
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-23.18.02-1024x336.png)
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: totalResults > 0 ? Text('News ($totalResults)') : Text('News')
),
);
}
![](https://thana.in.th/wp-content/uploads/2021/06/Simulator-Screen-Shot-iPhone-12-Pro-2021-06-23-at-23.58.00-473x1024.png)
![](https://thana.in.th/wp-content/uploads/2021/06/Screen-Shot-2564-06-23-at-23.57.15-1024x567.png)
body:
ListView.separated(itemBuilder: (BuildContext context, int index){
return ListTile (
title: Text(articles[index]['title']),
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
itemCount: articles.length
),
![](https://thana.in.th/wp-content/uploads/2021/06/Simulator-Screen-Shot-iPhone-12-Pro-2021-06-23-at-23.57.51-473x1024.png)
NewsPage.dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
class NewsPages extends StatefulWidget {
const NewsPages({Key? key}) : super(key: key);
@override
_NewsPagesState createState() => _NewsPagesState();
}
class _NewsPagesState extends State<NewsPages> {
List<dynamic> articles = [];
int totalResults = 0;
bool isLoading = true;
_getData()async {
try {
var url = Uri.parse(
'https://newsapi.org/v2/everything?q=apple&from=2021-06-22&to=2021-06-22&sortBy=popularity&apiKey=xxx');
var response = await http.get(url);
final Map<String, dynamic> news = convert.jsonDecode(response.body);
setState(() {
articles = news['articles'];
totalResults = news['totalResults'];
isLoading = false;
});
} catch(e) {
setState(() {
isLoading = false;
});
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
_getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: totalResults > 0 ? Text('News ($totalResults)') : Text('News')
),
body:
ListView.separated(itemBuilder: (BuildContext context, int index){
return ListTile (
title: Text(articles[index]['title']),
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
itemCount: articles.length
),
);
}
}
(Visited 159 times, 1 visits today)