17

I'm in the process of building an app which uses streambuilder heavily for getting realtime data from firestore across multiple pages in the app.

As there is no stream close / stream subscription cancellation methods, is it safe to assume that streambuilder will auto detach listening to Firestore document changes when the widget is disposed by framework? Are there any caveats to be aware of.

Thanks.

Raghu
  • 209
  • 1
  • 2
  • 6
  • 1
    `"As there is no stream close / stream subscription cancellation methods [...]"` - `StreamSubscription` has `cancel()`method (and `pause()` / `resume()`) – pskink Apr 13 '19 at 06:30
  • Streambuilder streamsubscription has these methods? I am ware that it exists outside the streambuilder. Can you please elaborate a bit more.Thanks. – Raghu Apr 13 '19 at 08:56
  • https://api.dartlang.org/stable/2.2.0/dart-async/StreamSubscription-class.html#instance-methods – pskink Apr 13 '19 at 08:58
  • Hi Pskink..Probably I will rephrase my question - Do we need to close stream subscription by ourselves when using stream builder or will it 'detach' itself when the widget is disposed? Sorry if my question is not clear. – Raghu Apr 13 '19 at 09:07
  • did you check [StreamBuilder](https://www.crossdart.info/p/flutter/0.0.38-dev/src/widgets/async.dart.html#line-129) sources? if so, whats unclear? – pskink Apr 13 '19 at 09:07

1 Answers1

19

A StreamBuilder closes its associated stream automatically when the widget subscribing to the stream is permanently removed from the widget tree:

https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/async.dart#L131

Or a permanent example if the link the master branch won't work one day:

https://github.com/flutter/flutter/blob/2ece1c3a174c3dd7099736f13b4ad16bb9b70c1d/packages/flutter/lib/src/widgets/async.dart#L131

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
  • What is really meant by that when you say "when the widget subscribing to the stream is permanently removed from the widget tree"? When is a widget permanently removed from a widget tree? I am asking this wrt the following question: https://stackoverflow.com/questions/71382905/flutter-ios-crash-with-exc-bad-access-error – edn Mar 18 '22 at 23:26