How does Streambuilder work when it receives a stream?

Viewed 458

Since I want to work with multiple streams in parallel, I am interested in how Streambuilder works exactly. Did I understand correctly that when Streambuilder receives a stream, it updates the build method?

Currently, when Streambuilder receives a stream, it updates the state of the entire widget each time. I thought it only updates the child Widget (in this case Text) in streambuilder and not the complete widget.

  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          StreamBuilder<Map>(
              stream: NumberCreator().stream,
              builder: (context, snapshot) {
                return Text(snapshot.data['test']);
              }),
          StreamBuilder<Map>(
              stream: OtherCounter().stream,
              builder: (context, snapshot) {
                return Text(snapshot.data['test2']);
              }),
        ],
      ),
    );
  }

I want to work with multiple streambuilders in a stack in a stateful widget. Is it better to outsource the streambuilders to their own stateless widgets or what would be a recommended architecture?

1 Answers

You can think of StreamBuilders as a pipe or wire or connector. At one "end there is a data" and the other "end is the event-triggered". The "event-trigger" here means the user asked for a data. Stream builder is async system so as long as it receives user input and it can retrieve the data, it will run automatically. So to me, it does not matter how you put it as long as you achieve what you want is better? The best or recommended way is implement after you have finished your way of code so you won't have a headache

Related