개발/Flutter

stream cancel 하기

dev_caleb 2022. 1. 23. 02:21
728x90

GetXcontroller에서 stream 걸어서 

원하는 Firestore document를 감시하곤 한다.

 

그런데 해당 페이지를 벗어나게 되었을 때 stream을 풀어줘야. 원치 않는 firebase 서버 요청을 줄일 수 있다고 생각한다. 

그래서 이번에는 stream을 cancel하는 방법을 알아보고자 한다.

 

아래와 같은 stackoverflow 답변을 참조하여 문제를 해결하였다.

https://stackoverflow.com/questions/44450758/cancel-stream-ondata

 

Cancel stream onData

I have an event bus that handles all central events to my app. I have a special case where I have a sequence of asynchronous actions that I want to execute once only (when a special event happens),...

stackoverflow.com

 

 

스트림을 아래와 같이 변수로 지정

StreamSubscription stream;
stream = inquireNetworkRepository.getChatRoomModelStream(makeruid).listen((newUserInquireRoom) async{
  pageState = PageState.Loading;
  logger.d("newUserInquireRoom 실행");
  userInquireRoom = newUserInquireRoom;
  await getFetchChatList(makeruid);
  pageState = PageState.Done;
});

삭제하고 싶을 때

if (stream!=null){stream.cancel();}

 

728x90