项目构建器未在futurbuilder Flutter中触发

问题描述

Scaffold(body: FutureBuilder(
      future: fetchTracks(),builder: (BuildContext context,AsyncSnapshot snapshot){

     if(snapshot.hasData)
     {
        ListView.builder(
    scrollDirection: Axis.vertical,itemExtent: 130.0,physics: AlwaysScrollableScrollPhysics(),shrinkWrap: true,itemCount: trackes.length,itemBuilder: (BuildContext context,int index) {
      print("test");
      return makeCard(snapshot.data[index]);
    },).build(context);
     }
     else
     {
       return Center(child: new CircularProgressIndicator());
     }

    } ));

当我调用此Scaffold Future构建时,将调用我的未来函数fetchTracks()并获取快照中的数据,但它没有进入itemBuilder函数。因此,futurebuilder返回NULL。 请帮助我解决。并提前谢谢

解决方法

您在return之前缺少ListView.builder。如果您不将其退还,它将无法构建它。

,

FutureBuilder 具有您必须处理的不同快照连接状态。在 ConnectionState 等于 done 且 hasData 等于 true 之前,流上的数据不可用。

_loadData(context)
 {
   showModalBottomSheet(
   context: context,builder: (BuildContext bc){ 
    return FutureBuilder(
    future: fetchTracks(),builder: (BuildContext context,AsyncSnapshot<List<MyClass>> snapshot){
        if (snapshot.connectionState!=ConnectionState.done)
        {
            return PleaseWaitWidget();
        }
        else if(snapshot.hasError)
        {
            DialogCaller.showErrorDialog(context,"future builder  has an error").then((value){});
        }
        else if (snapshot.connectionState==ConnectionState.done)
        {
          if(snapshot.hasData){ 

              List<Widget> list = snapshot.data.map((MyClass myClass){
                return Card(
        child:Wrap(children:<Widget>[
                  Row(children: [Text(myClass.field1)],),]));}).toList();

              return list;
          }
        }
    });
   });
  }