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

1,172

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.

Share:
1,172
Ajay Rn
Author by

Ajay Rn

Updated on December 15, 2022

Comments

  • Ajay Rn
    Ajay Rn over 1 year

    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?