Hoping someone(s) can help explain to me why this happens cause it's not making sense to me and has been annoyingly taken days to figure out what was causing this issue.
The basic situation is when I define the StreamController globally it works fine, but once I try to define it within function the StreamBuilder never gets notified of new data/messages:
class _MyHomePageState extends State<MyHomePage> {
static StreamController<bool?>? streamController = StreamController(); // Works
// static StreamController<bool?>? streamController; // Doesn't work
static void start() async {
// streamController = StreamController(); // Doesn't work
streamController!.sink.add(true);
}
@override
Widget build(BuildContext context) {
...
StreamBuilder<bool?>(
stream: streamController?.stream,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.active:
_counter++;
continue keepGoing;
keepGoing:
case ConnectionState.done:
case ConnectionState.waiting:
return Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
);
case ConnectionState.none:
return const CircularProgressIndicator();
return const Placeholder();
default:
return const Text("Unknown State of Stream.");
}
}),
FloatingActionButton(
onPressed: () => start(),
tooltip: 'Increment',
child: const Icon(Icons.play_arrow_outlined),
),
...
}
}
My current guess is that the reference to the StreamController in the StreamBuilder never gets the updated reference but I'm doubtful of that since I believe the Widget's state should get updated with the button press. Any other suggestions and explanations would be great.
Thanks in advance.