0

I am developing an app by combining riverpod and stream. However, I receive the same event twice. To avoid duplicate reception, the stream is listened to in initState. However, a duplicate event occurred.

I checked it by taking breakpoints in debug mode, and I saw that two identical events were raised in streamController almost at the same time.

//This is the code that listens to the stream.
@override
  void initState() {
    super.initState();

    ToiletListViewModel toiletListViewModel =
        ref.read(toiletListViewModelProvider.notifier);

    TextViewModel textViewModel = ref.read(textViewModelProvider.notifier);
    textViewModel.setTexts();

    toiletListViewModel.uiEventStream.listen((event) {
      event.when(
        onLoading: _onLoading,
        onError: _onError,
        onSuccess: _onSuccess,
      );
    });
    toiletListViewModel.getToiletListLocal();
    toiletListViewModel.getToiletListFromRemote();
  }
//This is the code that sends an event to the stream
Future getToiletListFromRemote() async {
    _uiEventController.add(const ToiletListUiEvent.onLoading());//This event occurs twice at a time.
    try {
      List<Toilet> results =
          await getToiletListFromRemoteUseCase(toiletListPage);
      state = [...state, ...results];
      _uiEventController.add(const ToiletListUiEvent.onSuccess());
      saveToiletList(state);
    } catch (e) {
      e as DioError;
      _uiEventController.add(ToiletListUiEvent.onError(e.message));
    }
    return state;
  }

If I make a mistake and fire the event twice, shouldn't success or fail occur twice as well as loading? In the code above, only the loading event is fired twice with no difference of 1ms.

What could be the cause? I don't know at all. Thank you for your help.

Queen Ellery
  • 481
  • 1
  • 6
  • 11

1 Answers1

0

sorry. This was a stupid mistake. toiletListViewModel.getToiletListLocal(); I was calling the same event here.

Queen Ellery
  • 481
  • 1
  • 6
  • 11