Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ListView中使用setState_Listview_Flutter_Dart_Setstate - Fatal编程技术网

在ListView中使用setState

在ListView中使用setState,listview,flutter,dart,setstate,Listview,Flutter,Dart,Setstate,我试图从两个表中获取数据并显示到ListView child: Container( child: StreamBuilder<List<ABC>>( stream: _abcBloc .getListStream, builder: (context, snapshot) {

我试图从两个表中获取数据并显示到ListView

 child: Container(
                  child: StreamBuilder<List<ABC>>(
                      stream: _abcBloc
                          .getListStream,
                      builder: (context, snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.waiting:
                            return Center(
                              child: Image.asset(
                                'assets/loading.gif',
                                width: 200.0,
                                height: 200.0,
                              ),
                            );
                            break;

                          case ConnectionState.active:
                            if (snapshot.data.isEmpty) {
                              return Container(child: Text('empty'));
                            } else {
                              return ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: snapshot.data.length,
                                  itemBuilder: (context, index) {
                                    var _reading = snapshot.data[index];
                                    var name;

                                    _abcBloc.getName(_reading.Id)
                                        .then((onValue) {
                                      name = onValue.name;
                                    });

                                    return InkWell(
                                        onTap: () {},
                                        child: Padding(
                                          padding: EdgeInsets.all(10),
                                          child: Column(
                                            children: <Widget>[
                                              Text(name == null ? '' : name),
                                            ],
                                          ),
                                        ));
                                  });
                            }
                            break;

                          default:
                            return Text("edwqd");
                        }
                      }))
首先,它将调用_abcBloc.getListStream。我能够得到这行的数据 var_reading=snapshot.data[index]


之后,我想通过调用此函数_abcBloc.getName_reading.Id从另一个表中获取名称。但是,名称始终为show empty。

无论何时使用Future并基于Future的价值创建小部件,都应始终使用FutureBuilder

用以下代码替换ListView.builder:

ListView.builder(
  shrinkWrap: true,
  itemCount: snapshot.data.length,
  itemBuilder: (context, index) {
  var _reading = snapshot.data[index];
  var name;

  // _abcBloc.getName(_reading.Id)
  //     .then((onValue) {
  //   name = onValue.name;
  // });

  return FutureBuilder(
    future: _abcBloc.getName(_reading.Id),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if(!snapshot.hasData) return Container();
      name = snapshot.data;
      return InkWell(
      onTap: () {},
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            Text(name == null ? '' : name),
          ],
        ),
      ))
    },
  );
});
希望这有帮助,如果有任何疑问,请发表评论。
如果对你有效,别忘了接受答案并投票。

我已经更新了我的答案,希望能有所帮助。如果有任何疑问,请发表评论。试试这个:name=wait\u abcBloc.getName\u reading.Id;它应该可以工作,因为name被用作紧跟其后的文本的值。