在StreamBuilder中更新数据

时间:2020-10-15 11:09:11

标签: flutter asynchronous setstate stream-builder

我有以下代码:

StreamBuilder<List<Job>>(
          stream: _jobBloc.userJobsObservable,
          builder: (BuildContext context,
              AsyncSnapshot<List<Job>> userJobsSnapshot) {
            Widget widget;
            if (userJobsSnapshot.hasData) {
              widget = ListView.builder(
                itemCount: userJobsSnapshot.data.length,
                itemBuilder: (BuildContext context, int index) {
                  _jobBloc.getJobOffers(userJobsSnapshot.data[index].key);
                  var totalOffers = 0;
                  return Column(
                        children: [
                          Text(userJobsSnapshot.data[index].someText),
                          StreamBuilder<Map<User, List<Offer>>>(
                              stream: _offerBloc.getJobOffersObservable,
                              builder: (context, jobOffersSnapshot) {
                                Widget offersWidget;
                                if (jobOffersSnapshot.hasData) {
                                  jobOffersSnapshot.data.forEach((key, value) {
                                    totalOffers += value.length;
                                  });
                                  offersWidget = Text(totalOffers > 0
                                                        ? totalOffers.toString()
                                                        : "0 applications");
                                } else {
                                    offerWidget = Container(
                                      color: Colors.white,
                                      child: Center(
                                        child: CircularProgressIndicator(),
                                      ),
                                    );
                                  }
                                  return offerWidget;
                                }), // close StreamBuilder
                         ]); // close Column
                 }) // close ListView.builder
               } // close if
               else {
               widget = Container(
                 color: Colors.white,
                 child: Center(
                   child: CircularProgressIndicator(),
                   ),
                 );
               }
             return widget;
           }), // close first StreamBuilder

我有一个具有以下结构的数据库:

Job collection - userId (who posted the job)
                     |-- jobID
                           |-- job data
User collection - userId
                     |-- user data
Offer collection - jobId
                     |-- userId (who posted the offer)
                          |-- offerId
                               |-- some data about offer ...

在第一个StreamBuilder中,我打电话来获取作业列表。 然后,我会在文本上打印一些工作信息,然后在下面显示该工作的要约数量以及发布要约的用户个人资料图片(这就是为什么我将“用户”用作地图的键)。因此,我有另一个StreamBuilder,可以为每个用户将报价分组。

然后我进入每个组以添加要约长度列表以获取总数,但是,由于未设置状态,因此文本始终会打印0个应用程序。

如果我尝试setState(),则在构建窗口小部件时收到我尝试设置setState的错误。

如何根据jobOffersSnapshot包含的内容显示不同的文本?

0 个答案:

没有答案