Stream is very important in FLUTTER. In the current market, the state management framework of FLUTTER is basically implemented with stream streams

The Stream classification

Flows can be divided into two categories:

Single Subscription streams, which can have at most one listener, Broadcast streams, which can have multiple listeners

Stream creation

There are many ways to create a stream, stream.fromFuture, stream.fromiterable, etc., but we don’t normally use StreamController

  • StreamController: Used to control the Stream process as described by the class name, providing interfaces for creating various streams of events
  • StreamSink: Generally used as an entry point for events, such as Add,addStream, etc
  • Stream: The source of the event itself. It can be used to listen for events or transfer events, such as LISTEN and WHERE.
  • StreamSubscription: An event subscription object that is ostensibly used to manage subscribed operations such as cancel and pause, but is also an internal event relay key.

Single subscription stream created

StreamController<String> StreamController = StreamController(); streamController.stream.listen((data)=> print(data)); streamController.sink.add("11111"); streamController.add("2222"); streamController.close();Copy the code

// Multiple subscription streams are created

StreamController<String> streamController = StreamController.broadcast();
    streamController.stream.listen((data){
      print(data);
    },onError: (error){
      print(error.toString());
    });
    streamController.stream.listen((data) => print(data));
    streamController.add("bbb");
Copy the code

// Single subscription streams are multiple subscription streams

  StreamController<String> streamController = StreamController();
  Stream stream =streamController.stream.asBroadcastStream();
  stream.listen((data) => print(data));
  stream.listen((data) => print(data));
  streamController.sink.add("aaa");
  streamController.close();
Copy the code

The StreamController stream creation is common above

High order use

EventBus uses good stuff

Class EventBusHelper {static final EventBus _eventBus = new EventBus(); EventBusHelper._(); static void fire(dynamic event) { _eventBus.fire(event); } static Stream<EventType> getEventTypeStream<EventType>() { return _eventBus.on<EventType>(); } } class ImageBackEvent { final int code; ImageBackEvent(this.code); } // accept @override void initState() {super.initstate (); this._loadHistoryData(); StreamSubscription stream. = EventBusHelper getEventTypeStream < ImageBackEvent > (), listen ((event) {/ / can do oneself operation}); } // send eventBusHelper.fire (ImageBackEvent(1))Copy the code

EventBus source

import 'dart:async'; class EventBus { StreamController _streamController; /// Controller for the event bus stream. StreamController get streamController => _streamController; EventBus({bool sync = false}) : _streamController = StreamController.broadcast(sync: sync); EventBus.customController(StreamController controller) : _streamController = controller; Stream<T> on<T>() {if (T == dynamic) {return streamController.stream; } else { return streamController.stream.where((event) => event is T).cast<T>(); } } /// Fires a new event on the event bus with the specified [event]. /// void fire(event) { streamController.add(event); } /// Destroy this [EventBus]. This is generally only in a testing context. /// void destroy() { _streamController.close(); }}Copy the code