2

I have ScreenA with CubitA and StateA. From ScreenA, I am opening a modal bottom sheet and passing the CubitA through BlocProvider.value(). I also have a BlocBuilder inside the BottomSheet widget.

I can use CubitA correctly from inside the sheet (e.g: call a method inside when button pressed), but the state in the BottomSheet won't update after it changes.

Code

showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (_) {
        final cubit = Provider.of<CubitA>(context);
        return BlocProvider.value(
          value: cubit,
          child: BottomSheetContent(),
        );
      },
    );

BottomSheetWidget

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(30, 20, 30, 20),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30),
            topRight: Radius.circular(30),
          )),
      child: SafeArea(
        top: false,
        child: BlocBuilder<OffersMapCubit, OffersMapState>(
            bloc: context.watch<OffersMapCubit>(),
            builder: (context, state) {
              THIS IS ONLY CALLED ONCE AT STARTUP BUT NEVER AGAIN
              return Content()...
              );
            }),
      ),
    );
  }

According to the documentation (https://pub.dev/packages/flutter_bloc), by using watch or Provider.of(), widgets in the subtree should listen for changes.

Is this expected behavior? Am I doing something wrong?

Any help is appreciated, thanks!

Daniel Ocampo
  • 236
  • 2
  • 13
  • I used the same `BuildContext` in provider and did not attention to `builder: (_)` which doesn't inject context from the builder and used widget context – zex_rectooor May 30 '23 at 08:55

1 Answers1

0

I had the same issue. You need to dispose of the cubit once you close the bottom sheet. The behaviour is that the bottom sheet widget is disposed of once closed, hence we need to recreate our cubit afresh.