flutter bloc dispose while using get_it

2.5k views Asked by At

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

1

There are 1 answers

0
Zakir On BEST ANSWER

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.