Bloc: getBloc() & getRepository()

Created on 30 Nov 2020  Â·  1Comment  Â·  Source: felangel/bloc

I want to suggest idiomatic useful functions :)

T getBloc<T extends Cubit<Object>>(BuildContext context) =>
    BlocProvider.of<T>(context);

T getRepository<T>(BuildContext context) => RepositoryProvider.of<T>(context);

usage:

getBloc<LoginCubit>(context).logInWithCredentials();

// ...

SignUpCubit(getRepository<AuthenticationRepository>(context));
question waiting for response

Most helpful comment

Hi @comerc 👋

Thanks for the suggestions! There are already existing extension methods on BuildContext for this.
You can use context.read<LoginCubit>() or context.read<AuthenticationRepository>().

Hope that helps! ✌

>All comments

Hi @comerc 👋

Thanks for the suggestions! There are already existing extension methods on BuildContext for this.
You can use context.read<LoginCubit>() or context.read<AuthenticationRepository>().

Hope that helps! ✌

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nerder picture nerder  Â·  3Comments

timtraversy picture timtraversy  Â·  3Comments

shawnchan2014 picture shawnchan2014  Â·  3Comments

RobPFarley picture RobPFarley  Â·  3Comments

rsnider19 picture rsnider19  Â·  3Comments