'How to map a stream that depends on another stream to a new model depending on both streams?

I have 2 Firestore streams where the 2nd one depends on the 1st one. I want to emit new models that depend on data from both streams.

In pseudocode you can think of it in a following way:

// 1st stream emits some device data
final streamA = client.device(id: 0); // Stream<DeviceData>

// 2nd stream emits some metadata for the above device type
final streamB = client.deviceMetadata(type: device.type); // Stream<DeviceMetadata>

// and finally I want to receive stream of combined device (device data + metadata)
final combinedObject = Device.from(deviceData, deviceMetadata);

However, I cannot find an effective way of mapping a stream onto another stream while accessing the value from the source stream. For instance with .map() I would like to write:

return client.device('deviceId')
   .map((device) => client.deviceMetadata(type: device.type))
   .map((deviceMetadata) => Device.from(???, deviceMetadata));

The problem is of course that I don't know how to access device in the second map().

What stream transformation or rxdart extension I could use to achieve that?



Solution 1:[1]

You can use the rxdart library to combine multiple streams into one stream https://pub.dev/documentation/rxdart/latest/rx/CombineLatestStream-class.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yair Chen