【Flutter】合并多个流Stream

1.说明

无意间发现了一个好用的库rxdart,它为 Dart 的 Stream 添加了额外的功能。

2.功能

(1)合并多个流Stream

借助Rx.combineLatest2()合并两个流stream1和stream2

注意:如果dart文件中同时使用了getx,需要隐藏掉Rx,否则会冲突。

dart 复制代码
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
// import 'package:get/get.dart' hide Rx;

// Library: rxdart , v0.28.0
// 为 Dart 的 Stream 添加了额外的功能
// 合并多个流 CombineLatestStream

// Publisher: fluttercommunity.dev

class RxdartDemo extends StatefulWidget {
  const RxdartDemo({super.key});

  @override
  State<RxdartDemo> createState() => _RxdartDemoState();
}

class _RxdartDemoState extends State<RxdartDemo> {
  //(热重载报错) Unhandled Exception: Bad state: Stream has already been listened to.
  // 解决:更改为广播流(broadcast)。
  StreamController<String> streamController1 = StreamController<String>.broadcast();
  late Stream<String> stream1;

  StreamController<int> streamController2 = StreamController<int>.broadcast();
  late Stream<int> stream2;

  StreamController<List<int>> streamController3 = StreamController<List<int>>.broadcast();
  late Stream<List<int>> stream3;

  @override
  void initState() {
    super.initState();

    stream1 = streamController1.stream;
    streamController1.add("A");

    stream2 = streamController2.stream;
    streamController2.add(1);

    stream3 = streamController3.stream;
    streamController3.add([1, 2]);
  }

  @override
  void dispose() {
    streamController1.close();
    streamController2.close();
    streamController3.close();

    super.dispose();
  }

  // Rx.combineLatest2 将两个Stream流合并
  Stream<String> get streamCombined =>
      Rx.combineLatest2(stream1, stream2, (a, b) => '$a - $b');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              "单个流Stream",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(
              height: 12,
            ),
            StreamBuilder<List<int>>(
                stream: stream3,
                builder: (context, snapshot) {
                  return Text(
                    "${snapshot.data}",
                    style: const TextStyle(fontSize: 16),
                  );
                }),
            const SizedBox(
              height: 12,
            ),
            const Text(
              "合并多个流Stream",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(
              height: 12,
            ),
            StreamBuilder<String>(
                stream: streamCombined,
                builder: (context, snapshot) {
                  return Text(
                    snapshot.data ?? 'empty',
                    style: const TextStyle(fontSize: 16),
                  );
                }),
            TextButton(
                onPressed: () {
                  streamController1.add("b");
                  streamController2.add(2);
                  streamController3.add([3, 4]);
                },
                child: const Text(
                  'Change',
                  style: TextStyle(fontSize: 16, color: Colors.blue),
                ))
          ],
        ),
      ),
    );
  }
}
相关推荐
Jewel1054 小时前
Flutter代码混淆
android·flutter·ios
一头小火烧13 小时前
flutter打包签名问题
flutter
sunly_13 小时前
Flutter:异步多线程结合
flutter
AiFlutter13 小时前
Flutter网络通信-封装Dio
flutter
B.-13 小时前
Flutter 应用在真机上调试的流程
android·flutter·ios·xcode·android-studio
有趣的杰克13 小时前
Flutter【04】高性能表单架构设计
android·flutter·dart
sunly_1 天前
Flutter:父组件,向子组件传值,子组件向二级页面传值
flutter
爱学习的绿叶1 天前
flutter TabBarView 动态添加删除页面
flutter
趴菜小玩家1 天前
使用 Gradle 插件优化 Flutter Android 插件开发中的 Flutter 依赖缺失问题
android·flutter·gradle
jhonjson2 天前
Flutter开发之flutter_local_notifications
flutter·macos·cocoa