1import 'dart:convert';
2import 'package:http/http.dart';
3
4class NetworkHelper {
5 String url;
6 NetworkHelper(this.url);
7
8 Future getData() async {
9 Response response = await get(Uri.parse(url));
10
11 if (response.statusCode == 200) {
12 return jsonDecode(response.body);
13 } else {
14 print(response.statusCode);
15 }
16 }
17}
18
19//To use the class
20
21//IMPORT THE CLASS FIRST
22NetworkHelper networkHelper = NetworkHelper('YOUR_URL');
23var data = await networkHelper.getData();
24
1 import 'dart:async';
2import 'dart:convert';
3import 'package:flutter/material.dart';
4import 'package:clickmeetplay/iam/user/postbean.dart';
5import 'package:http/http.dart' as http;
6
7class PostHome extends StatelessWidget {
8 @override
9 Widget build(BuildContext context) {
10 return MaterialApp(home: Scaffold(body: PostScreen(),),);
11 }
12}
13
14class PostScreen extends StatefulWidget {
15 @override
16 _PostScreenState createState() => _PostScreenState();
17}
18
19class _PostScreenState extends State<PostScreen> {
20
21 List<Post> _postList =new List<Post>();
22
23 Future<List<Post> > fetchPost() async {
24 final response =
25 await http.get('http://alkadhum-col.edu.iq/wp-json/wp/v2/posts/');
26
27 if (response.statusCode == 200) {
28 // If the call to the server was successful, parse the JSON
29 List<dynamic> values=new List<dynamic>();
30 values = json.decode(response.body);
31 if(values.length>0){
32 for(int i=0;i<values.length;i++){
33 if(values[i]!=null){
34 Map<String,dynamic> map=values[i];
35 _postList .add(Post.fromJson(map));
36 debugPrint('Id-------${map['id']}');
37 }
38 }
39 }
40 return _postList;
41
42 } else {
43 // If that call was not successful, throw an error.
44 throw Exception('Failed to load post');
45 }
46 }
47 @override
48 Widget build(BuildContext context) {
49 return Container();
50 }
51
52 @override
53 void initState() {
54
55 fetchPost();
56
57 }
58}