Shady Aziza
Shady Aziza

Reputation: 53327

Clean way of sharing data between BLoCs

Imagine the following pseudo blocs:

class BlocA {
  BlocA() {
     //some initialization
   }
   Stream a;
   Stream b;
   Stream c;
   }


class BlocB {
  BlocB() {
    //some initialization
  }
  Stream d; //dependant on a piece of data that resides in BlocA
}

What is the cleanest way of passing information from one bloc to another, how to handle this dependency?

Upvotes: 3

Views: 4175

Answers (1)

Rémi Rousselet
Rémi Rousselet

Reputation: 277127

You can inject as parameter to your BlocB the BlocA.

class BlocA {
  Stream a;
}

class BlocB {
  final BlocA _a;

  BlocB(this._a);
}

Alternatively, you can pass only the stream you need instead of the whole block. But do that only if the number of streams is very limited.

You can then freely map/pipe streams from A to expose a different one into B.

class BlocB {
  Stream b;

  BlocB(BlocA blocA) {
    b = blocA.a.map((a) => 42).asBroadcastStream();
  }
}

Upvotes: 6

Related Questions