showing results for - "how to display data from json api using flutter expansiontile"
Giorgia
26 May 2017
1import 'dart:async';
2import 'package:intl/intl.dart';
3
4import 'package:flutter/material.dart';
5import 'package:flutter/services.dart';
6//import 'package:shared_preferences/shared_preferences.dart';
7import 'package:http/http.dart' as http;
8//import 'package:cswauthapp/models.dart';
9import 'package:flutter/foundation.dart';
10import 'dart:convert';
11
12var jsonCodec = const JsonCodec();
13List<Exp> myReasonList;
14List myDCList;
15int mycount = 0;
16
17void main() {
18  runApp(new MyApp());
19
20}
21
22
23
24class MyApp extends StatelessWidget {
25  @override
26  Widget build(BuildContext context) {
27    return new MaterialApp(
28      title: 'ExpansionTile Test',
29      home: new MyHomePage(),
30    );
31  }
32}
33
34class MyHomePage extends StatefulWidget {
35  @override
36  _MyHomePageState createState() => new _MyHomePageState();
37}
38
39class _MyHomePageState extends State<MyHomePage> {
40
41  @override
42  void initState() {
43    super.initState();
44
45    _getData();
46    //_getSpecialty();
47  }
48
49  _getData() async {
50    var _url = 'http://$baseurl:8080/support/dc/1';
51
52    var http = createHttpClient();
53    var response = await http.get(_url);
54
55    var dc = await jsonCodec.decode(response.body);
56    myDCList = await dc.toList();
57
58
59
60    print('DC: '+myDCList.toString());
61
62    if (mounted) {
63      setState(() {
64        //_dataReceived = true;
65        mycount = myDCList.length;
66      });
67    }
68
69  }
70
71  Future _getChildren(int did) async {
72
73    var _url2 = 'http://174.138.61.246:8080/support/dcreasons/$did';
74    var http = createHttpClient();
75    var response = await http.get(_url2);
76    var reasons = await jsonCodec.decode(response.body);
77    myReasonList = await reasons.toList();
78    print('REASONS: '+ myReasonList.toString());
79
80
81    return myReasonList;
82  }
83
84  @override
85  Widget build(BuildContext context) {
86    return new Scaffold(
87      appBar: new AppBar(
88        title: new Text('ExpansionTile Test'),
89      ),
90      body: new ListView.builder(
91        itemBuilder: _itemBuilder,
92        itemCount: mycount,
93      ),
94    );
95  }
96
97  Widget _itemBuilder(BuildContext context, int index) {
98    Exp exp = getExp(index);
99    return new ListChild(exp: exp,);
100  }
101
102  Exp getExp(int index) {
103    return new Exp(
104      myDCList[index]['dname'],
105      _getChildren(myDCList[index]['did']),
106    );
107    //return new Specialties.fromMap(mylist[index]);
108
109  }
110}
111
112class Exp {
113  Exp(this.title, [this.children]);
114  final String title;
115  final Future<List<Exp>> children;
116}
117
118
119class ListChild extends StatefulWidget {
120  ListChild({Key key, this.exp}) : super(key: key);
121
122  final Exp exp;
123  @override
124  State createState() => new ListChildState();
125}
126
127class ListChildState extends State<ListChild> {
128  //PageStorageKey<ListChildState> _key = new PageStorageKey(ListChild);
129  @override
130  Widget build(BuildContext context) {
131    return new ExpansionTile(
132      key: new PageStorageKey(ListChild),
133      title: new Text(widget.exp.title),
134      children: <Widget>[
135        new Text(widget.exp.children.title),
136      ],
137    );
138  }
139}
140