BLoC: listen callback called multiple times


I have a Stateful Widget which obtains a bloc from its parent:

class Page extends StatefulWidget {
  State<StatefulWidget> createState() =>

class _PageState extends State<Page> {
  final TextEditingController mnemonicController = TextEditingController();
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  Widget build(BuildContext context) {

final MnemonicLogicBloc mnemonicLogicBloc =

  if (!isCorrect) {
    SnackBar copySnack = SnackBar(content: Text('Wrong Mnemonic!'));

return Scaffold(
    key: _scaffoldKey,
    body: Container(
      width: double.infinity,
      child: Column(
        children: <Widget>[
            child: TextField(
              controller: mnemonicController,
            onPressed: () {
            child: Text('Check', style: TextStyle(color: Colors.white)),

What I want to do is to check, when user press the button, if the string inserted is correct. The method I call in the bloc is:

void isMnemonicCorrect(String typedMnemonic){
if(typedMnemonic == _lastGeneratedMnemonic)

_inCheckMnemonic is the Sink of my Subject (I’m using rxDart), while outCheckMnemonic is my Stream. The problem is that, even though the bloc’s ‘isCorrect’ method is called once, the listen callback is called twice (The SnackBar is shown twice). Why does this happen?

EDIT: I navigate to Page() simply using Navigator.push:

                            builder: (BuildContext context) {
                return Page();

I’m able to retrieve the bloc because, when my app start I initialized the bloc:

return runApp(BlocProvider<ApplicationBloc>(
bloc: ApplicationBloc(),
child: BlocProvider<MnemonicLogicBloc>(
  bloc: MnemonicLogicBloc(),
  child: BlocProvider<HomePageBloc>(
    bloc: HomePageBloc(),
    child: App(),



When you add a new Widget in your Navigator avoid creating that widget inside the builder, instead declare a variable outside and reuse that variable in your builder, like this:

final page = Page();
                            builder: (BuildContext context) => page ), );

With this change we avoid that our widget is recreated many times in an unexpected way.

Answered By – diegoveloper

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.