Bloc: How to Pass object in BlocProvider.of

Created on 23 Mar 2020  路  5Comments  路  Source: felangel/bloc

Hi,
I want to pass some object/data while calling particular event using the BlocProvider.of.

For example BlocProvider.of

Is this possible?

Thanks,
Sathya.

question

Most helpful comment

Hi @sathya4code 馃憢
Thanks for opening an issue!

You can pass arguments to the bloc via the constructor when it's created

BlocProvider(
  create:(_) => AuthenticationBloc(data: data),
  child: ...
),

Or if the data is more dynamic as @Ahmzyjazzy mentioned you should pass the data to the bloc via an event

BlocProvider.of<AuthenticationBloc>(context).add(SomethingHappened(data:data));

Hope that helps 馃憤

@felangel, thank you. I will try this.

All 5 comments

If your custom event expect an object/data you can pass it using the snippet below

BlocProvider.of<AuthenticationBloc>(context).add(SomeEvent(data: data));

The above applicable if your Event is using a name param. Otherwise you can perform same operation like this.

BlocProvider.of<AuthenticationBloc>(context).add(SomeEvent(data));

Hi @Ahmzyjazzy ,

I want to pass the param to AuthenticationBloc.

example BlocProvider.of

Hi @sathya4code 馃憢
Thanks for opening an issue!

You can pass arguments to the bloc via the constructor when it's created

BlocProvider(
  create:(_) => AuthenticationBloc(data: data),
  child: ...
),

Or if the data is more dynamic as @Ahmzyjazzy mentioned you should pass the data to the bloc via an event

BlocProvider.of<AuthenticationBloc>(context).add(SomethingHappened(data:data));

Hope that helps 馃憤

Hi @Ahmzyjazzy ,

I want to pass the param to AuthenticationBloc.

example BlocProvider.of

@sathya4code you can implement this as mentioned by @felangel above. Bloc dependency arguments are passed through the constructor when it is created.

Hi @sathya4code 馃憢
Thanks for opening an issue!

You can pass arguments to the bloc via the constructor when it's created

BlocProvider(
  create:(_) => AuthenticationBloc(data: data),
  child: ...
),

Or if the data is more dynamic as @Ahmzyjazzy mentioned you should pass the data to the bloc via an event

BlocProvider.of<AuthenticationBloc>(context).add(SomethingHappened(data:data));

Hope that helps 馃憤

@felangel, thank you. I will try this.

Was this page helpful?
0 / 5 - 0 ratings