· 5 years ago · Jun 20, 2020, 01:24 PM
1import 'package:flutter/material.dart';
2import 'package:http/http.dart' as http;
3import 'dart:async';
4import 'dart:convert';
5
6
7void main() => runApp(MyApp());
8String url = "https://jsonplaceholder.typicode.com/posts/1";
9// For Http Request Here ok bro
10Future<Post> fecthPost() async {
11 final response = await http.get(url);
12 if (response.statusCode == 200 ) {
13 return Post.fromJson(json.decode(response.body));
14 }else{
15 throw Exception('Failed to make request');
16 }
17}
18
19// Make Claa Post Model
20class Post {
21 final int userId;
22 final int id;
23 final String title;
24 final String body;
25
26 Post({this.userId, this.id, this.title, this.body});
27
28 factory Post.fromJson(Map<String, dynamic> json) {
29 return Post(
30 userId: json['userID'],
31 id: json['id'],
32 title: json['title'],
33 body: json['body']
34 );
35
36 }
37
38}
39
40class MyApp extends StatelessWidget {
41 // This widget is the root of your application.
42 @override
43 Widget build(BuildContext context) {
44 return MaterialApp(
45 title: 'Flutter Demo',
46 theme: ThemeData(
47 // This is the theme of your application.
48 //
49 // Try running your application with "flutter run". You'll see the
50 // application has a blue toolbar. Then, without quitting the app, try
51 // changing the primarySwatch below to Colors.green and then invoke
52 // "hot reload" (press "r" in the console where you ran "flutter run",
53 // or simply save your changes to "hot reload" in a Flutter IDE).
54 // Notice that the counter didn't reset back to zero; the application
55 // is not restarted.
56 primarySwatch: Colors.red,
57 ),
58 home: MyHomePage(title: 'Get API'),
59 );
60 }
61}
62
63class MyHomePage extends StatefulWidget {
64 MyHomePage({Key key, this.title}) : super(key: key);
65
66 // This widget is the home page of your application. It is stateful, meaning
67 // that it has a State object (defined below) that contains fields that affect
68 // how it looks.
69
70 // This class is the configuration for the state. It holds the values (in this
71 // case the title) provided by the parent (in this case the App widget) and
72 // used by the build method of the State. Fields in a Widget subclass are
73 // always marked "final".
74
75 final String title;
76
77 @override
78 _MyHomePageState createState() => _MyHomePageState();
79}
80
81class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
82 // Make constructor
83 final Future<Post> post = fecthPost() ;
84
85 @override
86 Widget build(BuildContext context) {
87 // This method is rerun every time setState is called, for instance as done
88 // by the _incrementCounter method above.
89 //
90 // The Flutter framework has been optimized to make rerunning build methods
91 // fast, so that you can just rebuild anything that needs updating rather
92 // than having to individually change instances of widgets.
93 return Scaffold(
94 appBar: AppBar(
95 // Here we take the value from the MyHomePage object that was created by
96 // the App.build method, and use it to set our appbar title.
97 title: Text(widget.title),
98 ),
99 body: Center(
100 child: FutureBuilder<Post>(
101 future: post,
102 builder: (context, snapshot){
103 if(snapshot.hasData){
104 print(snapshot.data.title);
105 return Text(snapshot.data.title, textAlign: TextAlign.center, style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red, fontSize: 20.0));
106 }
107 else if(snapshot.hasError){
108 return Text("${snapshot.error}", style: TextStyle(fontWeight: FontWeight.bold),);
109 }
110 else{
111 return CircularProgressIndicator(backgroundColor: Colors.amber,);
112 }
113 },
114
115 ),
116 )
117 );
118 }
119}