stream

自带的:

 

单一

 

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

  

posted @ 2019-05-09 17:33  CrossPython  阅读(367)  评论(0编辑  收藏  举报