首页 > 其他 > 详细

stream

时间:2019-05-09 19:13:39      阅读:102      评论:0      收藏:0      [点我收藏+]

自带的:

 

单一

 

class DataBloc {
  StreamController dataBloc = StreamController();
  StreamSink get dataSink => dataBloc.sink;
}

  

addData(){
    dataBloc.dataSink.add(‘addsfdb‘);
  }

  



StreamBuilder(
            stream: dataBloc.dataBloc.stream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                return Text(‘${snapshot.data}‘);
              }else{
                return Text(‘first‘);
              }
            },
          ),

  

 

多次

 

class DataBloc {
  StreamController dataBloc;
  DataBloc(){
    dataBloc = StreamController.broadcast();
  }
  Stream get dataStream => dataBloc.stream;
  StreamSink get dataSink => dataBloc.sink;
}

  

StreamBuilder(
            stream: dataBloc.dataStream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                print(‘this first ${snapshot.data}‘);
                return Text(‘${snapshot.data}‘);
              }else{
                return Text(‘first‘);
              }
            },
          ),

          StreamBuilder(
            stream: dataBloc.dataStream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                print(‘this seconds. ${snapshot.data}‘);
                return Text(‘${snapshot.data}‘);
              }else{
                return Text(‘second‘);
              }
            },
          ),

  

 addData(){
    dataBloc.dataSink.add(‘addsfdb‘);
  }

  

 

rxdart:

class DataBloc {
  PublishSubject dataBloc = PublishSubject();
}

  

StreamBuilder(
            stream: dataBloc.dataBloc.stream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                print(‘this first ${snapshot.data}‘);
                return Text(‘${snapshot.data}‘);
              }else{
                return Text(‘first‘);
              }
            },
          ),

          StreamBuilder(
            stream: dataBloc.dataBloc.stream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                print(‘this seconds. ${snapshot.data}‘);
                return Text(‘${snapshot.data}‘);
              }else{
                return Text(‘second‘);
              }
            },
          ),

  

addData(){
    dataBloc.dataBloc.add(‘xyz‘);
  }

  

stream

原文:https://www.cnblogs.com/pythonClub/p/10839848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!