DEV Community

ThinkDigitalSoftware
ThinkDigitalSoftware

Posted on

Facepalm moment of the day

I'm developing an app in Flutter and Dart and I come across an issue where an event being added to my BLoC. (A stream-based state management solution for the uninitiated)

The state class is made up of a combination of a few different streams. When one releases an event, the state is updated with the latest updated value using a copyWith() function. This is all going fine and well until I push a null value into one of the streams and the state doesn't change. I know it was pushed successfully, but the old state is still reflected. Hopefully this will help some of you.

If your copyWith functions look like this:

State copyWith({int param1, String param2}){
  return State(
            param1: param1 ?? this.param1, 
            param2: param2 ?? this.param2
         ); 

then passing a null value will cause it to return the existing value even if you intended to set it to null. I doubt this is an issue in languages with an Undefined type, however, Dart does not have one.

Top comments (0)