Marko Žilić
Marko Žilić

Reputation: 35

Flutter - how to cancel a SSE Stream

Hey I cant figure out how to cancel an SSE Stream in my flutter app.

I am using this package to use SSE in my app: https://pub.dev/packages/sse_channel

I feel like it has a major lack in the documentation.

The SSE works fine just don't know how to stop it with a function.

Im using this to start the SSE

channel = SseChannel.connect(Uri.parse(dotenv.env['BASE_URL'] + '/sse/rn-updates'));

and

channel.stream.listen((message) { code... and some other stuff }

that part works but I don't know how to close it.

Tried stuff like this

static void cancelStream() {
if (channel != null)
channel.sink.close()
}

Upvotes: 0

Views: 721

Answers (1)

ARASHz4
ARASHz4

Reputation: 311

You should create a global var in your class

StreamSubscription? stream;

and assign it to this variable

stream = channel.stream.listen((message) { code... and some other stuff }

and you can use this code to cancel stream

stream?.cancel();

Upvotes: 1

Related Questions