flutter StreamSubscription subscriber stream

When you use [Stream.listen] to listen to [Stream]
then the [StreamSubscription] object is returned

List<StreamSubscription?> subscriptions = [];

  @override
  void initState() {
    
    
    super.initState();
    
//subscriptions列表添加两个StreamSubscription。Stream.listen返回StreamSubscription对象
    subscriptions
      ..add(ZegoUIKit().getAudioVideoListStream().listen(
      //onStreamListUpdated相当于(event){onStreamListUpdated(event)},接收到数据时触发回调
        onStreamListUpdated,
        onDone: () {
    
    //streamController.sink.close(); //只有手动调用close方法发送一个done事件,onDone才会被回调
          print('测试视频控件1-------------getAudioVideoListStream-onDone');
        },
        onError: (e) {
    
    //有错误时会回调
          print('测试视频控件1-------------getAudioVideoListStream-onError${
    
    e}');
        }
      ))
      
      ..add(
          ZegoUIKit().getScreenSharingListStream().listen(onStreamListUpdated,    onDone: () {
    
    
            print('测试视频控件1-------------getScreenSharingListStream-onDone');
          },
              onError: (e) {
    
    
                print('测试视频控件1-------------getScreenSharingListStream-onError${
    
    e}');
              }));
  }
//流会执行且返回List<ZegoUIKitUser>数据
  Stream<List<ZegoUIKitUser>> getAudioVideoListStream() {
    
    
    return ZegoUIKitCore.shared.coreData.audioVideoListStreamCtrl.stream
        .map((users) => users.map((e) => e.toZegoUikitUser()).toList());
  }
//监听数据:获取到流返回的数据后执行下面代码,如果没返回数据就不会执行这里
  void onStreamListUpdated(List<ZegoUIKitUser> streamUsers) {
    
    
    print('测试视频控件1---streamUsers${
    
    streamUsers}');

    fullScreenUserNotifier.value = ZegoUIKit().getScreenSharingList().isEmpty
        ? null
        : ZegoUIKit().getScreenSharingList().first;

    setState(() {
    
    
      updateUserList(
        ZegoUIKit().getAudioVideoList() + ZegoUIKit().getScreenSharingList(),
      );
    });

Flutter asynchronous programming-Stream
The difference between Future and stream (Future returns an asynchronous result. Stream returns multiple results and continues to monitor)
Future represents a calculation process that will not be completed immediately. Unlike ordinary functions that directly return results, asynchronous functions return a Future that will contain the results. This Future notifies the caller when the result is ready. A Stream is a sequence of asynchronous events. It is similar to an asynchronous Iterable (iterable), the difference is that when you get the next event from the Iterable it will give it to you immediately, but the Stream will not give it to you immediately but when it is ready

You can use a restaurant dining scene to understand the difference between Future and Stream:
Future is like going to a restaurant to eat. After ordering the dishes you want to eat at the front desk, you pay. After paying, the waiter will give you a waiting number plate (equivalent to getting a Future first), and the chef will start cooking according to the order you placed. When your dish is ready, you can get the designated number through the number plate. (returned data or exception information). Stream is like going to a restaurant to eat. After ordering the 4 dishes A, B, C, and D you want to eat at the front desk (subscribing to the data stream process), then you go to the table and wait. As for when the dishes will be ready, you don’t know. You know it, so you keep waiting (similar to listening all the time), and the chef will start cooking according to the order you placed. When your first plate of A is ready, the waiter will take the initiative to send A to you. On the table (based on a similar subscription-push mechanism), there are no special surprises. The order of dishes pushed by the waiter should also be A, B, C, D.

stream returns data and monitors data

//Stream implementation
void main() async { Stream stream = Stream.periodic(Duration(seconds: 1), (int value) { return value + 1;//stream increases by 1 every second }); await stream.forEach((element ) => print('stream value is: $element'));//The results can be obtained through steam }




Guess you like

Origin blog.csdn.net/weixin_44911775/article/details/133670215