项目生成器未在 futurbuilder Flutter 中触发

Item builder not getting triggered in 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 构建时,将调用我的 future 函数 fetchTracks() 并获取快照中的数据,但它不会进入 itemBuilder 函数。所以 futurebuilder return NULL。 请帮我解决。提前谢谢你

您在 ListView.builder 之前缺少 return。如果您不 return 它,它就不会构建它。

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;
          }
        }
    });
   });
  }