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