I have defined one button inside one StatelessWidget (This will have the bloc creation logic and injecting using bloc provider, ), on click of the button i am showing a dialog and passing the bloc instance to it, as shown in the code.


//EsignBloc is defined here in parent statelessWidget. Defined i.e. creating the bloc instance and passing through the BlocProvider. Removed the code for simplicity

//This listener will be called when Button defined inside statelessWidget will be clicked. this is responsible for showing the dialog.
void _onClickHere(
    BuildContext context,
  ) {
    final dialog = Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(AppConstants.borderRadius),
      ),
      elevation: 0.0,
      backgroundColor: Colors.transparent,
      child: _GetSignUsingOtpView(),
    );

    showDialog(
      context: context,
      builder: (_) => BlocProvider<EsignBloc>(
        create: (_) => BlocProvider.of<EsignBloc>(context), // passing already created bloc to dialog
        child: WillPopScope(
          onWillPop: () => Future.value(false),
          child: dialog,
        ),
      ),
      barrierDismissible: false,
    );
  }

Pasting some code of _GetSignUsingOtpView()

class _GetSignUsingOtpView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<EsignBloc, EsignState>(builder: (context, state) {
      return Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.circular(
            AppConstants.borderRadius,
          ),
        ),
        child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Align(
                  alignment: Alignment.centerRight,
                  child: InkWell(
                    onTap: () => _closeDialog(context),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 8.0, right: 8),
                      child: Icon(
                        Icons.cancel,
                        color: AppColor.primaryDark,
                        size: SizeConfig.safeBlockVertical * 2,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(24),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      PrimaryText(text: state.otp), // data does not change after closing and opeing dialog again 
                      PrimaryText(text: state.remainingTime), // data does not change after closing and opeing dialog again 
                    ],
                  ),
                ),
              ],
            ),
      );
    });
  }

  void _closeDialog(BuildContext context) {
    Navigator.pop(context);
  }
}

The problem that I am facing is whenever the dialog opens again after closing, it doesn't show the latest data from the bloc. The dialog just shows whatever previous data is in the bloc. Can someone point it out, where i am making the mistake?


Solution 1: mrgnhnt96

The reason that your dialog is not showing new data is because you are creating a new instance of the bloc. Even though you say that you're not.

BlocProvider<EsignBloc>(
  create: (_) => BlocProvider.of<EsignBloc>(context), // passing already created bloc to dialog
  child: ...

In some cases, BlocProvider can be used to provide an existing cubit to a new portion of the widget tree. This will be most commonly used when an existing cubit needs to be made available to a new route. In this case, BlocProvider will not automatically close the cubit since it did not create it.

To not create a new instance, you need to use BlocProvider.value. You can read more about it here

BlocProvider.value(
  value: BlocProvider.of<EsignBloc>(context),
  child: ...,
);

Instead of creating a new instance, this will use the previous instance and provide it to the child widget and will NOT close the bloc when its done using it. This is handy for dialogs and navigation.