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..

10
  • 1
    add print(snapshot) before return Container( - what do you see on the logs? Commented Jan 14, 2021 at 5: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"? Commented Jan 14, 2021 at 13:52
  • you mean that snapshot is null? - it is impossible Commented Jan 14, 2021 at 14:03
  • 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 Commented Jan 14, 2021 at 14:17
  • 1
    hmmm, then i would refer to some good tutorials explaining Provider package ;-) Commented Jan 14, 2021 at 14:30

1 Answer 1

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
      ),
    );
  },
);
Sign up to request clarification or add additional context in comments.

4 Comments

I thought by calling _categoryStream.updateCategory(currentChosenCategory) i would add data to the stream?
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.
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..
Could you post your entire code including the part where you called _categoryStream.updateCategory()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.