Closed appano1 closed 2 years ago
Thanks for reporting. I'll fix this ASAP.
But, in advance. The overrideWithValue
is misused because the CounterCubit('sub')
is not disposed.
You can use counterProvider.overrideWithProvider(BlocProvider<CounterCubit, int>((ref) => CounterCubit('sub')))
.
I replied the error. I was surprised, but there are 2 mistakes in your code:
1) Prefer override with provider:
final counterProvider = BlocProvider<CounterCubit, int>(
(_) => throw UnimplementedError(),
);
Widget build(BuildContext context) {
return ProviderScope(
overrides: [
counterProvider.overrideWithValue(
BlocProvider((ref) => CounterCubit('sub'))
),
],
child: Container(),
);
}
or its equivalent:
final counterProvider = BlocProvider<CounterCubit, int>(
(_) => CounterCubit('sub'),
);
Widget build(BuildContext context) {
return ProviderScope(
overrides: [counterProvider], // auto overrides itself
child: Container(),
);
}
2) The methods must be always called as following (with either riverbloc
or flutter_bloc
)
DONT pass the methods as arguments.
DO create a callback that reads the bloc and calls the method.
Wrap(
spacing: 12,
children: [
ElevatedButton(
onPressed: () => ref.read(counterProvider.bloc).increase(),
child: const Text('+1'),
),
ElevatedButton(
onPressed: () => ref.read(counterProvider.bloc).decrease(),
child: const Text('-1'),
),
],
),
These steps are required to ensure the store is initialized. Remember that its initialization is asynchronous and need some micro or millisecond to be ready.
Thank you for letting me know the mistake... Thanks a lot..! 😁
You are welcome. Thanks for using riverbloc
.
I'm using riverbloc with hydrated_bloc.
HydratedBloc throws StorageNotFound error after hot reloaded even the storage was initialized.
example
error