I think the function you are looking for is Stream.asyncMap instead of Stream.map. I am assuming that the multiple subscription commands
are arriving as a stream, as the example code seems to show.
You can also construct a stream using an async* function - that can be easier to read.
If your input is a stream of commands, that you want to asynchronously fetch a subscription for, and then put all
the events from that subscription's stream onto the result stream, before fetching the next subscription, then I think something
like this should work. The await for loop will pause the stream commandInputSubject while it is in the body of the loop, forwarding the events from the current subscription.
Stream<T> eventsFromCommands(Stream<Command> commandInputSubject) async* { await for (final command in commandInputSubject) {
final subscribed = await client.subscribe(command);
yield* subscribed.stream;
}
}
The function Stream.asyncMap can apply an async function to a stream's elements, and await those functions as part of the mapping process, pausing the input stream.
And the function Stream.asyncExpand can expand a stream of streams into a single stream. So this could also be written with
final subscriptions = _commandInputSubject.asyncMap((command) => client.subscribe(command));
final events = subscriptions.asyncExpand((subscribed) => subscribed.stream);
I haven't tested these answers, so there may be minor mistakes. If you don't want to wait for one subscription to complete
before starting the next command, then you will have to use different code and decide how you want to interleave the events.