Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Flutter StreamBuilder which allows me to listen to multiple streams?

Tags:

flutter

dart

Is there a StreamBuilder which allows me to listen to multiple streams? something like:

   body: MultiStreamBuilder(
        streams: [bloc.state.stream, bloc.kind.stream],
        builder: (context) {

I don't need the snapshot like in the Flutter StreamBuilder because I can just read from the bloc

like image 233
Ride Sun Avatar asked Oct 25 '19 04:10

Ride Sun


People also ask

What is the difference between StreamBuilder and FutureBuilder in Flutter?

FutureBuilder solves a square value and returns the result after 5 seconds, till then we show a progress indicator to the user. StreamBuilder shows a stopwatch, incrementing _count value by 1 every second.

When should I use StreamBuilder Flutter?

The StreamBuilder widget is used in many kinds of Flutter applications, especially chat applications, social networks, real-time content updates, etc. In this article, we will go over 2 complete examples of implementing StreamBuilder: the first example is a real-time clock app and the second one is a demo chat app.

How many streams does Flutter have?

There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default. They work well when you're only using a particular stream on one screen. A single subscription stream can only be listened to once.


1 Answers

This is updating the UI (calling build) whenever one of the streams gets updates. I don't use the snapshot.data because I am reading the data directly from the bloc and snapshot.data contains only a bool not the real data of the streams.

class _RemoteDebuggingScreenState extends State<RemoteDebuggingScreen> {
  @override
  Widget build(BuildContext context) {
    RemoteDebugBlog bloc = BlocProvider.of(context).remoteDebugBloc;

    return Scaffold(
      body: StreamBuilder(
        stream: Observable.combineLatest2(bloc.state.stream, bloc.kind.stream,
            (b1, b2) => b1 != null || b2 != null),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Container();

          return Column(...

for more info with some nice graphics about combineLatest check this out: https://www.burkharts.net/apps/blog/rxdart-magical-transformations-of-streams/

Now knowing the above I moved this into a Widget:

import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';

class DoubleStreamBuilder extends StatelessWidget {
  @override
  DoubleStreamBuilder(
      {@required this.stream1, @required this.stream2, @required this.builder});

  final Stream stream1;
  final Stream stream2;
  final Widget Function(BuildContext) builder;

  Widget build(BuildContext context) => StreamBuilder(
      stream: Rx.combineLatest2(
          stream1, stream2, (b1, b2) => b1 != null || b2 != null),
      builder: (context, snapshot) => builder(context));
}


The usage is now clear and simple like this:

 return DoubleStreamBuilder(
    stream1: settingsBloc.uiVin.stream,
    stream2: settingsBloc.isPseudoVin.stream,
    builder: (context) {
   => this updates when stream 1 or 2 have new values
   }
like image 103
Ride Sun Avatar answered Sep 25 '22 03:09

Ride Sun