0

I am facing an issue in my Bloc. In the app, user can filter by multiple values. When I use filter, transition is done correctly, but when I used filter2, nothing is happening and state is not rebuilt in BlocBuilder. Am I missing something? What is the difference between these two approaches (one working and another not)?

Example method:

Stream<EventFilteredState> _mapUpdateFilterToState(
    UpdateFilter event,
  ) async* {
    if (eventsBloc.state is EventsLoaded) {
      final events = (eventsBloc.state as EventsLoaded).events;

      final filter = [event.faculty]..addAll((state as FilteredEventsLoaded).faculties);
      final filter2 = (state as FilteredEventsLoaded).faculties..add(event.faculty);

      yield FilteredEventsLoaded(events, filter);
    }
  }
Bondolin
  • 2,793
  • 7
  • 34
  • 62
Stepan
  • 1,041
  • 5
  • 23
  • 35

2 Answers2

1

I forgot the place I read about it so I can't explain the problem but I can name it: The difference is that you create a completely new array in filter. In filter2 you only edit the array of your state. Sometimes the comparison is not working correctly because of that. The state thinks it is the same array after you yielded your edited array.

Edit: I just realized your two filters do the same. So I deleted the rest of my answer.

Edit 2: You can read more about the comparison of lists here, f.e.: How can I compare Lists for equality in Dart? Just google "flutter array compare".

Bondolin
  • 2,793
  • 7
  • 34
  • 62
Sonius
  • 1,597
  • 3
  • 14
  • 33
  • Thanks for your answer. I am quite confused about it. It should be the same. Do you think it can be related with Equatable package I am using for comparing? – Stepan Dec 19 '19 at 13:37
0

I found the solution. Never manipulate the state; create a new one. In this case, I created a copy of that array and posted to FilteredEventsLoaded. It just works.

Bondolin
  • 2,793
  • 7
  • 34
  • 62
Stepan
  • 1,041
  • 5
  • 23
  • 35