Home > Back-end >  A value of type 'Stream<dynamic>' can't be returned from the function because i
A value of type 'Stream<dynamic>' can't be returned from the function because i

Time:09-28

After migrating to flutter 2, the following section of code no longer works:

class ColorBloc extends BlocBase {
// streams of Color
  StreamController _streamListController = StreamController<Color>.broadcast();
// sink
  Sink get colorSink => _streamListController.sink;
// stream
  Stream<Color> get colorStream => _streamListController.stream;


// function to change the color
  changeColor(String chosenColour) {
    switch(chosenColour) {
      case 'blackTheme':
        {
          colorSink.add(AppState.blackTheme);
        }
        break;

      case 'blueTheme':
        {
          colorSink.add(AppState.blueTheme);
        }
        break;

      case 'greenTheme':
        {
          colorSink.add(AppState.greenTheme);
        }
        break;

      case 'redTheme':
        {
          colorSink.add(AppState.redTheme);
        }
        break;

      case 'whiteTheme':
        {
          colorSink.add(AppState.whiteTheme);
        }
        break;
    }

  }

  @override
  dispose() {
    _streamListController.close();
  }
}

The issue being with this line:

 Stream<Color> get colorStream => _streamListController.stream;

And the reason given is:

A value of type 'Stream<dynamic>' can't be returned from the function 'colorStream' because it has a return type of 'Stream<Color>'. 

But I am not really sure what this means, or how to go about trying to resolve it.

CodePudding user response:

You just need to edit this line StreamController _streamListController = StreamController<Color>.broadcast(); to StreamController<Color> _streamListController = StreamController<Color>.broadcast();

The error says that colorStream is a Stream of Color but you are giving it a Stream of dynamic and dynamic could be anything. So it's a wrong type assignment. Stream of course is generic.

CodePudding user response:

Try adding the Type Color to your StreamController like so:

StreamController<Color> _streamListController = StreamController<Color>.broadcast();
  • Related