本文介绍了Dart流.asBroadcastStream内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的Flutter应用程序中,内存泄漏和流没有关闭.我们将源代码追溯到以下代码:

In our Flutter application we have memory leaks and streams not being closed. We traced the source to code such as:

Rx.combineLatest(...).asBroadcastStream()

RxDart .combineLatest()的结果是单订阅流.添加.asBroadcastStream()可使流方便地用于我们的各种Flutter显示器.但是,当这些显示器关闭时,合并的流仍处于活动状态.

The result of RxDart .combineLatest() is a single-subscription stream. Adding .asBroadcastStream() makes the stream conveniently available to our various Flutter displays. However when these displays are closed the streams being combined are still active.

推荐答案

来自.asBroadcastStream()文档:

因此,根据设计,流一直存在到被明确取消为止.要在最后一个侦听器取消后取消订阅,请使用:

So by design the stream exists until explicitly cancelled. To cancel the subscription when the last listener cancels use:

Rx.combineLatest(...).asBroadcastStream( onCancel: (sub) => sub.cancel() )

Stream.asBroadcastStream-容易引起泄漏,这是什么?原理? #26686

这篇关于Dart流.asBroadcastStream内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:47