flutter: BehaviorSubject not rebuild widget

Issue

I am trying to use Rxdart on my statless widget:

class SimplePrioritySelectWidget extends StatelessWidget {
  BehaviorSubject<List<String>> _valueNotifier =
      BehaviorSubject<List<String>>.seeded([]);

I wrap my widget by StreamBuilder:

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: _valueNotifier.stream,
      initialData: options,
      builder: (context, snapshot) {
        print("rebuild");
        return Padding(
          padding: const EdgeInsets.only(top: 25),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 16.h,
              ),

I have a custom drop down widget, I don’t know why, when I add a string inside _valueNotifier the builder method not called? and my widget not rebuilded? What is wrong?

  CustomDropdown(
    dropdownMenuItemList: options,
    enableBorderColor: Color(PRIMARY_COLOR_2),
    onChanged: (value) {

      _valueNotifier.value.add(value);
    
    },

  ),

Solution

I totally agree that the
you need to use sink in _valueNotifier

  CustomDropdown(
    dropdownMenuItemList: options,
    enableBorderColor: Color(PRIMARY_COLOR_2),
    onChanged: (value) {
      _valueNotifier.sink.add([value]);
    },

  ),

Answered By – Sami Kanafani

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *