In Android, we use broadcast, eventBus and other ways to achieve monitoring and messaging. In Flutter we use a Stream to achieve the same capability. The code for Flutter is as follows:
Stream abstract class EventStreamController {//Message is the body of the Message in the stream, Is a custom class static StreamController<Message> _streamNotice; static Stream<Message> get noticeStream { if (_streamNotice == null) { _streamNotice = StreamController<BucketNoticeMessage>.broadcast(); } return _streamNotice.stream; } // provide external calls to send messages static sendMessage(Message MSG) {_streamNotice? .sink? .add(msg); } // Custom Message body (generic) abstract Class Message{}Copy the code
Register to listen for messages
EventStreamController. NoticeStream. Listen ((event) {if (the event is a Message) {/ / judging generic, carries on the corresponding operation}});Copy the code
The use of Stream in Flutter is very extensive and will be added later.