Q:

StreamBuilder doesn't rebuilds after stream changed by setState

I have a StatefulWidget in wich, there is a variable called stream that is initialized to an initial stream:
var stream = stream1;

When setState is called:
setState(() {
 condition ? stream = stream1 : stream = stream2;
});

Thesee streams are consumed by a StreamBuilder:
StreamBuilder(
 stream: stream,
 builder: (context, snap) {
  return RaisedButton(
   child: ..
   onPressed: snap.hasData ? () => my_function() : null
  );
 }
)

The problem is that when the builder function gets the data by the stream and the button becomes clickable and then the state changes, the new StreamBuilder button remains clickable though the second stream doesn't have data infact if a click the button my_function() isn't being executed.

A:

The issue is actually not that StreamBuilder doesn't rebuild, but that it preserves the last emitted event.
As such, you cannot simply check snapshot.hasData. You will have to check snapshot.connectionState too.
When StreamBuilder receives a new stream, snapshot.connectionState will reset to ConnectionState.waiting until at least one value is emmited.
So instead of:
onPressed: snap.hasData ? () => my_function() : null

you can do:
onPressed: snap.connectionState != ConnectionState.waiting && snap.hasData ? () => my_function() : null

