简体   繁体   中英

Flutter: ListView.builder inside FutureBuilder grows infinitely

I want to fetch datas from an online open data interface and then show them on the screen using ListView.builder. So, I create a FutureBuilder which returns a ListView.builder. The data has a exact length, but the ListView seems to grow infinitely. Is there a infinity loop or any bug in FutureBuilder which caused this problem?

The part of the code which caused this problem(I think):

Widget build(BuildContext context) {

    final key = utf8.encode(AppKey);
    final hmac = hmacSha1(key, utf8.encode(SignDate));
    final base64HmacString = base64Encode(hmac);

    final Authorization = "hmac username=\"" + AppID + "\", algorithm=\"hmac-sha1\", headers=\"x-date\", signature=\"" + base64HmacString + "\"";

    getNumData();
    for(int i = 0 ; i < Numdata.length ; i++){
      if(Numdata[i]['stationName'] == DStation){
        DStationNum = Numdata[i]['stationCode'];
        break;
      }
    }
    for(int i = 0 ; i < Numdata.length; i++){
      if(Numdata[i]['stationName'] == AStation){
        AStationNum = Numdata[i]['stationCode'];
        break;
      }
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(191, 62, 255, 1),
        title: Text('火車查詢'),
      ),
      body: FutureBuilder(
          future: getTableData(Authorization, xdate, DStationNum, AStationNum),
          builder: (context, snap){
            if(!snap.hasData){
              return Container();
            }
            List<dynamic> datas = [];
            datas = jsonDecode(snap.data.body);
            count1++;
            print('count1: ' + count1.toString());
            for(int i = 0 ; i < datas.length ; i++){
              count2++;
              print('count2: ' + count2.toString());
              Map<String, dynamic> DailyTrainInfo = datas[i]['DailyTrainInfo'];
              Map<String, dynamic> OriginStopTime = datas[i]['OriginStopTime'];
              Map<String, dynamic> DestinationStopTime = datas[i]['DestinationStopTime'];
              CText.add(DailyTrainInfo["TrainTypeName"]["Zh_tw"]);
              print('CText.length: ' + CText.length.toString());
              DEPTime1Text.add(OriginStopTime["DepartureTime"]);
              DEPTime2Text.add(DestinationStopTime["DepartureTime"]);
              TrainText.add(DailyTrainInfo["TrainNo"]);
            }
            return ListView.builder(
              shrinkWrap: true,
              itemCount: CText.length,
              itemBuilder: (context, index){
                print('CText.length: ' + CText.length.toString());
                count3++;
                print('count3: ' + count3.toString());
                return ListTile(title: Text(CText[index] + '      ' + DEPTime1Text[index] + '   =>   ' + DEPTime2Text[index]), subtitle: Text(TrainText[index]),);
              },
            );
          }),
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped,
        currentIndex: _currentIndex, // this will be set when a new tab is tapped
        items: [
          new BottomNavigationBarItem(
            icon: new Icon(Icons.school),
            title: new Text('大學'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.directions_subway),
            title: new Text('交通'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.person),
            title: new Text('個人'),
          )
        ],
      ),
    );
  }

The getNumData() function:

Future getNumData() async{
    final String hostNum = 'http://ods.railway.gov.tw/tra-ods-web/ods/download/dataResource/0518b833e8964d53bfea3f7691aea0ee';
    final response = await http.get(hostNum);
    Numdata = jsonDecode(utf8.decode(response.bodyBytes));
  }

By default, a ListView expands to fill the available height. To make it shrink, set the shrinkWrap parameter to true . This should solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM