how to display data from json api using flutter expansiontile 1

how to display data from json api using flutter expansiontile

import 'dart:async';
import 'package:intl/intl.dart';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
//import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
//import 'package:cswauthapp/models.dart';
import 'package:flutter/foundation.dart';
import 'dart:convert';

var jsonCodec = const JsonCodec();
List myReasonList;
List myDCList;
int mycount = 0;

void main() {
  runApp(new MyApp());

}



class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'ExpansionTile Test',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State {

  @override
  void initState() {
    super.initState();

    _getData();
    //_getSpecialty();
  }

  _getData() async {
    var _url = 'http://$baseurl:8080/support/dc/1';

    var http = createHttpClient();
    var response = await http.get(_url);

    var dc = await jsonCodec.decode(response.body);
    myDCList = await dc.toList();



    print('DC: '+myDCList.toString());

    if (mounted) {
      setState(() {
        //_dataReceived = true;
        mycount = myDCList.length;
      });
    }

  }

  Future _getChildren(int did) async {

    var _url2 = 'http://174.138.61.246:8080/support/dcreasons/$did';
    var http = createHttpClient();
    var response = await http.get(_url2);
    var reasons = await jsonCodec.decode(response.body);
    myReasonList = await reasons.toList();
    print('REASONS: '+ myReasonList.toString());


    return myReasonList;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('ExpansionTile Test'),
      ),
      body: new ListView.builder(
        itemBuilder: _itemBuilder,
        itemCount: mycount,
      ),
    );
  }

  Widget _itemBuilder(BuildContext context, int index) {
    Exp exp = getExp(index);
    return new ListChild(exp: exp,);
  }

  Exp getExp(int index) {
    return new Exp(
      myDCList[index]['dname'],
      _getChildren(myDCList[index]['did']),
    );
    //return new Specialties.fromMap(mylist[index]);

  }
}

class Exp {
  Exp(this.title, [this.children]);
  final String title;
  final Future> children;
}


class ListChild extends StatefulWidget {
  ListChild({Key key, this.exp}) : super(key: key);

  final Exp exp;
  @override
  State createState() => new ListChildState();
}

class ListChildState extends State {
  //PageStorageKey _key = new PageStorageKey(ListChild);
  @override
  Widget build(BuildContext context) {
    return new ExpansionTile(
      key: new PageStorageKey(ListChild),
      title: new Text(widget.exp.title),
      children: [
        new Text(widget.exp.children.title),
      ],
    );
  }
}

Here is what the above code is Doing:
1. It’s calling the API to get the list of DCs.
2. It’s calling the API to get the list of reasons for each DC.
3. It’s building the list of DCs.
4. It’s building the list of reasons for each DC.

Here’s what I need to do:
1. I need to build the list of DCs.
2. I need to build the list of reasons for each DC.
3. I need to build the list of reasons for each reason.
4. I need to build the list of reasons for each reason for each reason.
5. I need to build the list of reasons for each reason for each reason for each reason.
6. I need to build the list of reasons for each reason for each reason for each reason for each reason.
7. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason.
8. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason.
9. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
10. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
11. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
12. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
13. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
14. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
15. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
16. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
17. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
18. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
19. I need to build the list of reasons for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason for each reason.
20. I need to build the list of reasons for each reason for each reason

Similar Posts