4

I am trying to create a "CategoryStream" to update the UI based on the users choice.

This is my stream:

import 'dart:async';

class CategoryStream {
  StreamController<String> _categoryStreamController =
  StreamController<String>();

closeStream() {
  _categoryStreamController.close();
}

updateCategory(String category) {
  print("Update category in stream was called");
  print("the new category is: " + category);
  _categoryStreamController.sink.add(category);
}

Stream<String> get stream => _categoryStreamController.stream;
}

And my StreamBuilder looks like this:

 return StreamBuilder<String>(
  stream: CategoryStream().stream,
  builder: (context, snapshot) {
    return Container(
      color: Colors.white,
      child: Center(
        child: Text(snapshot.data.toString()),
      ),
    );
  },
);

So when the User choses a new category, i try to update the Stream like this:

  CategoryStream().updateCategory(currentChosenCategory);

Whatever i do, the result is always null. Although the right category is displayed in the print() function... What am i missing?

Maybe i need to work with a StreamProvider? Not a StreamBuilder? Because i am adding the data from a Parent-Widget to a Child-Widget of a Child-Widget..

Arveni
  • 68
  • 6
  • 1
    add `print(snapshot)` before `return Container(` - what do you see on the logs? – pskink Jan 14 '21 at 05:19
  • only null. The thing is, i'd like to provide more code, but the widgets are too large to post here. I think i might be working with the wrong instance of the stream? Because i created _categoryStream in both widgets. Wouldn't both instances work "with the same stream"? – Arveni Jan 14 '21 at 13:52
  • you mean that `snapshot` is `null`? - it is impossible – pskink Jan 14 '21 at 14:03
  • My mistake. I printed snapshot.data... print(snapshot) prints: AsyncSnapshot(ConnectionState.waiting, null, null) – Arveni Jan 14 '21 at 14:11
  • 1
    yes, you are right, you cannot create multiple instances of `CategoryStream` with hope they will all refer to the same stream - thats why you have only "waiting" state – pskink Jan 14 '21 at 14:17
  • But how do i pass the new category with the help of a stream to a child widget? – Arveni Jan 14 '21 at 14:20
  • I tried to pass the _categoryStream with the help of a StreamProvider to the child-Widget. Therefore i used the StreamProvider.value-Widget. But i cant listen to Provider.of(context) ....because thats always null – Arveni Jan 14 '21 at 14:28
  • 1
    hmmm, then i would refer to some good tutorials explaining `Provider` package ;-) – pskink Jan 14 '21 at 14:30
  • Ok i think my knowledge still lacks a lot :D i dont think i will fix this problem too soon. Still, thanks :) – Arveni Jan 14 '21 at 14:31
  • good luck then! – pskink Jan 14 '21 at 14:32

1 Answers1

2

By default, the value of a StreamController is null. You need to set at initialData or add data to the stream before you call snapshot.data in your StreamBuilder:

 final CategoryStream _categoryStream = CategoryStream();
 return StreamBuilder<String>(
  stream: _categoryStream.stream,
  initialData: "",
  builder: (context, snapshot) {
    return Container(
      color: Colors.white,
      child: Center(
        child: Text(snapshot.data), //toString() is unnecessary, your data is already a string
      ),
    );
  },
);
CoderUni
  • 5,474
  • 7
  • 26
  • 58
  • I thought by calling _categoryStream.updateCategory(currentChosenCategory) i would add data to the stream? – Arveni Jan 14 '21 at 01:44
  • Yes it adds data to the stream but then you have many minor errors that might have caused the value to be null. Did my answer fix your problem? If not, post the logs. – CoderUni Jan 14 '21 at 02:00
  • I should have mentionend, that _categoryStream.updateCategory(currentChosenCategory) is called in another Widget. Is this a problem? It should connect to the same stream, shouldnt it? And no, it still does not work. But there are no erros. I think i just didnt understand streams properly.. – Arveni Jan 14 '21 at 02:05
  • Could you post your entire code including the part where you called _categoryStream.updateCategory()? – CoderUni Jan 14 '21 at 02:37