0

I tried getting input(string) from the user using textfield, adding the input in stream, and printing the string on a different screen, but instead of the string stream builder is printing null, I understood, it's because stream doesn't have any data. But I added data in stream once the user types out something in textfield and hit save.How do I love this

Stream

StreamController NameStream = StreamController.broadcast();

Adding data to stream

textField()
textbuttion(
 onPressed: () {
                  addNStream() {
                    NameStream.sink.add(TextController);
                  }

                  setState(() {
                    addNStream();
                  });

)

Printing data in different screen

Scaffold(
  body: Container(
      child: StreamBuilder(
    stream: NameStream.stream,
    builder: (context, snapshot) {
      return Center(
        child: Text(
          snapshot.data.toString(),
          style: const TextStyle(fontSize: 40),
        ),
      );
    },
  )
  ),
)
Ganesh Sivakumar
  • 149
  • 1
  • 16
  • you need to setup `StreamBuilder.initialData` or check `snapshot.connectionState` / `snapshot.hasData` (see [StreamBuilder](https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html) official docs - they have some examples on how to do that) – pskink Mar 26 '22 at 06:53
  • I used snapshot.hasData , it printed, no data in stream – Ganesh Sivakumar Mar 26 '22 at 06:57
  • post your code then – pskink Mar 26 '22 at 06:58
  • I did, in question section – Ganesh Sivakumar Mar 26 '22 at 08:02
  • i dont see any `snapshot.hasData` – pskink Mar 26 '22 at 08:04
  • I tried this, terminal output was no data. `StreamBuilder( stream: addBorrowerNameStream.stream, builder: (context, snapshot) { if (snapshot.hasData) { print("has data"); } else { print("no data"); } return Center( child: Text( snapshot.data.toString(), style: const TextStyle(fontSize: 40), ), ); }, )` – Ganesh Sivakumar Mar 26 '22 at 09:51
  • because you use `snapshot.data.toString()` even if `snapshot.hasData` is set to `false` – pskink Mar 26 '22 at 09:53
  • Could please explain more.. – Ganesh Sivakumar Mar 26 '22 at 09:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243343/discussion-between-ganesh-sivakumar-and-pskink). – Ganesh Sivakumar Mar 26 '22 at 09:56
  • you return `Text(snapshot.data.toString()` in both cases - if `snapshot.hasData` is true and false – pskink Mar 26 '22 at 09:57
  • That statement was just to check if stream has data. Just want to print string in screen but it printed null. So, used that condition statement. – Ganesh Sivakumar Mar 26 '22 at 10:03
  • it printed null because `snapshot.hasData` is false - it means there is no data: data is null, `hasData` official documentation says: *"Returns whether this snapshot contains a non-null data value. This can be false even when the asynchronous computation has completed successfully, if the computation did not return a non-null value. For example, a Future will complete with the null value even if it completes successfully."* – pskink Mar 26 '22 at 10:04
  • True, but I have added data(textController) into the stream right? why streamBuilder doesn't display that text. – Ganesh Sivakumar Mar 26 '22 at 10:12
  • `class StreamBuilderTest extends StatelessWidget { final ctrl = StreamController(); @override Widget build(BuildContext context) { return StreamBuilder( stream: ctrl.stream, builder: (context, snapshot) { return Center( child: ElevatedButton( onPressed: () => ctrl.add(DateTime.now().toString()), child: Text('press me, ${snapshot.data}'), ), ); } ); } }` – pskink Mar 26 '22 at 10:22
  • I still couldn't understand why it returns null, could you explain. – Ganesh Sivakumar Mar 26 '22 at 16:25
  • bacause you are using `snapshot.data` when you cannot do that - you can ONLY use `snapshot.data` when `snapshot.hasData` is set to `true` – pskink Mar 26 '22 at 16:33
  • Yeah, but why snapshot doesn't have any data, I've added data in stream. So, snapshot should data right? – Ganesh Sivakumar Mar 26 '22 at 16:42

2 Answers2

2

Looks like you're using a broadcast stream. Items added to a broadcast stream when nothing is listening are silently discarded.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
1

when you are using broadcast() your StreamBuilder don't listen to the data in the NameStream.stream in the initialization.

because the NameStream.stream doesn't have any listeners so StreamBuilder assume that the is no data so he doesn't check, this is the reason for the null.

to solve the problem you need to add listener (initSatate) before you add the event to the stream this will allow StreamBuilder to recognize that there is data in the stream.

ilyes chicha
  • 111
  • 2
  • 9