flutter bloc dispose while using get_it

Issue

I have a question about do we need to dispose of a bloc while creating it in the get_it package? as an example, I have bloc named LoginBloc.and I created it like :

final GetIt sl = GetIt.instance;
sl.registerFactory(() => LoginBloc(sl(), sl()));

and I provide it like:

    BlocProvider(
       child: LogInPage(),
          create: (BuildContext context) {
                return sl<LoginBloc>();
               }

my question is that does bloc provider dispose of bloc while the page is disposed or not needed or I must dispose of it manually cuz I used the get_it package to create it?
thank you all

Solution

BlocProvider will automatically handles the closing of bloc when used with create.

You can see the official document for more.
https://pub.dev/documentation/flutter_bloc/latest/flutter_bloc/BlocProvider-class.html

When you register something via sl.registerFactory, GetIt will create new instance every time you access it.

Answered By – Zakir

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *