Home > Back-end >  How to get value from StreamController?
How to get value from StreamController?

Time:08-04

How to get value 'data' from StreamController

stream.listen((data) {print('$data');}) ;

// global.dart

final StreamController ctrl = StreamController();

// father.dart

onSubmitted: (value) {
    debugPrint('Send Data: ' areaNameTextController.text);
    ctrl.sink.add('Receive Data: ' areaNameTextController.text);},

//children.dart

GoogleMap(
...
onTap: (LatLng latlng){
 ctrl.stream.listen((data) {
  print('$data');} ) ;
  
  Marker(
  ...
  infoWindow:InfoWindow(title:'///Where I want use data///'),
  );
   
  }
)

CodePudding user response:

You say StreamController and In your question you wroteStream. Should fix like that.

streamController.stream.listen(
  (event) => print('Event: $event'),
  onDone: () => print('Done'),
  one rror: (error) => print(error),
);

CodePudding user response:

In my code I use like that for pull to do pull to refresh. Listening is only purpose for listening from coming data and not for action. If you want to check something that come from BLOC controller, first listening in init state and use with action from that checked data.

  void initState() {
    _houseVisitBloc.listAppHouseVisitStream().listen((ResponseOb res) {
      if (res.message == MsgState.data) {
        List<HouseVisitAppData> listenList = res.data;
        mainPullList = listenList;
        print(listenList.toString());
        if (mainPullList == listenList) {
          _refreshController.refreshCompleted();
        }
        if (res.data == MsgState.data) {
          _refreshController.loadComplete();
        }
      }
      if (res.data != MsgState.data) {
        _refreshController.loadNoData();
      }
    });
    super.initState();
  }
  • Related