1
+ import '../../../../Data/Models/home_page_model.dart' ;
2
+
1
3
import '../../../../Data/Repository/home_page_repo.dart' ;
2
- import '../../../Components/future_builder.dart' ;
3
4
import 'package:flutter/material.dart' ;
4
5
5
6
class MyHomePage extends StatefulWidget {
@@ -12,15 +13,43 @@ class MyHomePage extends StatefulWidget {
12
13
}
13
14
14
15
class _MyHomePageState extends State <MyHomePage > {
16
+ late Future <DemoModel > futureDemoModel;
17
+
18
+ @override
19
+ void initState () {
20
+ futureDemoModel = DemoRepo ().fetchData ();
21
+ super .initState ();
22
+ }
23
+
15
24
@override
16
25
Widget build (BuildContext context) {
17
26
return Scaffold (
18
27
appBar: AppBar (
19
28
title: Text (widget.title),
20
29
),
21
30
body: Center (
22
- child: FutureBuilderWidget (futureCall: DemoRepo ().fetchData ()),
31
+ child: FutureBuilder (
32
+ future: futureDemoModel,
33
+ builder: (BuildContext context, snapshot) {
34
+ if (snapshot.hasData) {
35
+ return buildDataLayout (snapshot.data as DemoModel );
36
+ } else if (snapshot.hasError) {
37
+ return Text (snapshot.error.toString ());
38
+ } else if (snapshot.connectionState == ConnectionState .waiting) {
39
+ return const Text ('Waiting....' );
40
+ } else if (snapshot.connectionState == ConnectionState .none) {
41
+ return const Text ('Cannot establish connection with the server.' );
42
+ }
43
+ return const Center (child: CircularProgressIndicator ());
44
+ },
45
+ ),
23
46
),
24
47
);
25
48
}
49
+
50
+ Widget buildDataLayout (DemoModel data) => ListTile (
51
+ title: Text (data.userId.toString ()),
52
+ subtitle: Text (data.title),
53
+ trailing: Text (data.completed.toString ()),
54
+ );
26
55
}
0 commit comments