如何将未来的结果与流中的流相结合



im试图从数据库中获取数据并使用流传输该数据到屏幕。我使用FutureBuilder进行了此解决方案的工作原理,但是我需要使用streambuilder谁能帮我找到这个问题。我有一种从数据库获取数据的方法

Future<List<CementSandAggregateView>> getAllRatios() async{
   List<CementSandAggregateView> ratioList = new List();
    var types = await ratioRepository.getAll();
    for(int i = 0 ; i < types.length ; i ++){
      CementSandAggregateView view = new CementSandAggregateView();
      String cementRatio = types[i].cement.floor() < types[i].cement ? types[i].cement.toStringAsFixed(1):types[i].cement.toStringAsFixed(0);
      String sandRatio = types[i].sand.floor() < types[i].sand ? types[i].sand.toStringAsFixed(1):types[i].sand.toStringAsFixed(0);
      String aggregateRatio = types[i].aggregate.floor() < types[i].aggregate ? types[i].aggregate.toStringAsFixed(1):types[i].aggregate.toStringAsFixed(0);
      view.ratio = "$cementRatio:$sandRatio:$aggregateRatio (${types[i].name})";
      view.id = types[i].id;
      view.name = types[i].name;
      print(view.ratio);
      ratioList.add(view);
    }
    return ratioList;
  }

我想使用流

发送此数据
class CementSandAggregateMixBloC {
    BehaviorSubject<Future<List<CementSandAggregateView>>> _cSAMixController;
    BLoCProvider provider;
  CementSandAggregateMixBloC(){
    provider = new BLoCProvider();
    _cSAMixController = new BehaviorSubject<Future<List<CementSandAggregateView>>>();
    _cSAMixController.add(provider.getAllRatios());
  }
  Observable<Future<List<CementSandAggregateView>>> inputObservable() => _cSAMixController.stream;
    void dispose() {
    _cSAMixController.close();
  }
}

在我的屏幕中,我使用streambuilder

使用流
Widget _buildBottomNavigationBar1() {
    List<BottomNavigationBarItem> items = List();
    return StreamBuilder(
      stream: _rationBloC.inputObservable(),
      builder: (context,
          AsyncSnapshot<Future<List<CementSandAggregateView>>> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasData) {
            snapshot.data.then((onValue) => {
                  onValue.forEach((mix) => {})
                });
            return BottomNavigationBar(
                items: items,
                currentIndex: _selectedRatio,
                fixedColor: Theme.of(context).buttonColor,
                onTap: _onItemTapped);
          } else {
            return new Center(
              child: new CircularProgressIndicator(),
            );
          }
        } else {
          return new Container(width: 0.0,height: 0.0,);
        }
      },
    );
  }

但是数据不会来屏幕

我尝试了这个解决方案,但没有机会!

任何人可以帮助我将未来的结果转换为流?

谢谢。

实现这一目标的一种简单方法是await,即将完成,然后将数据放入流中。

在您的集团课程中:

void _init() async {
   List<CementSandAggregateView> data = await provider.getAllRatios();
   _cSAMixController.add(data);
}
CementSandAggregateMixBloC(){
    provider = new BLoCProvider();
    _cSAMixController = BehaviorSubject< List<CementSandAggregateView> >();
    _init();
  }

我认为_init()应该在StatefulWidgetinitState()中调用,或在initStatebuild方法中使用此代码。

WidgetBinding.instance.addPostFrameCallback(() => _init());

这意味着在渲染第一帧后,即build

最新更新