Effects of Injecting BLoC as a singleton using DI and where to close stream?
Asked Answered
F

1

7

I am using inject.dart to inject my bloc as a singleton, is it a bad practice i.e that causes memory leak?

Since all the streams I am using are Broadcast Streams from RxDart I was wondering if a singleton would work better?

And if not a bad practice, what is the best place to close the stream? Will doing it in Dispose() of stateful widgets(subscribing to bloc) cause issues since other widgets may be still using the stream?

Fontaine answered 31/10, 2019 at 13:40 Comment(0)
E
3

You shouldn't be using Singletons as it will consume too much memory. Instead register the bloc as factory class.

See this github issue - https://github.com/felangel/bloc/issues/711

If you register them as singleton once you dispose the bloc you cannot use the bloc again but if you register as factory you can dispose it in the void dispose(){...} method of the widget.

Evapotranspiration answered 29/3, 2020 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.