forked from AseemWangoo/flutter_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
futurebuilder_part1.dart
87 lines (78 loc) · 2.32 KB
/
futurebuilder_part1.dart
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; //for making request
import 'dart:async'; //for asynchronous features
import 'dart:convert';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new MyAppScreenMode();
}
}
class MyAppScreenMode extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Future Builder',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
appBar: new AppBar(
title: new Text('Future Builder'),
),
body: new Center(
child: new MiddleSection(),
)));
}
}
class MiddleSection extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new MiddleSectionState();
}
}
class MiddleSectionState extends State<MiddleSection> {
String display;
Widget futureWidget() {
return new FutureBuilder<String>(
future: getDataFB(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new Text(
snapshot.data.toString(),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: new TextStyle(fontWeight: FontWeight.bold),
);
} else if (snapshot.hasError) {
return new Text("${snapshot.error}");
}
return new CircularProgressIndicator();
},
);
}
@override
Widget build(BuildContext context) {
return new Container(
color: Colors.yellowAccent,
width: 250.0,
padding: const EdgeInsets.all(10.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Text("Future Builder Demo"),
futureWidget()
]));
}
}
Future<String> getDataFB() async {
http.Response response = await http.get(
Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"),
headers: {"Accept": "application/json"});
List data = JSON.decode(response.body);
print(data[1]['title'].toString());
return data[1]['title'].toString();
}