-1

I have a button in page 1 with onpressed that does

Navigator.push(context, customroutebuilerforanimation(page2()));

In page2 I have :

class Page2 extends StatefulWidget {
  const Page2({super.key});

  @override
  State<Page2> createState() => _Page2State();
}

class _Page2State extends State<Page2> {
  @override
  void initState() {
    super.initState();
    context.read<mybloc>().add(myblocevent());
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: BlocConsumer<myblocBloc, myblocState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        return Column(
          children: [
            if(state is something1) Placeholder(),
            if(state is something2) SizedBox.shrink(),
          ],
        );
      },
    ));
  }
}

Now when I open up the application and/or restart it and navigate to page2, the mybloc event does not get fired for some reason for the first time. please assume there is an APPBAR there that I missed to represent, that allows me to go back (navigator.pop()) and then come back to page2. When I do that, the event fires and results me the states that are not initial.

What can be the cause of this? I have tried looking hard, couldn't find the solution yet.

I am using GetIt, so the bloc is initialized and is provided in main, so no issues there.

Tried some work arounds , like the addpostframe callback method , did not work. Tried to put it inside of didchangedependencies() method, did not work. Tried to add it as a function in the page1 button's on pressed instead of the init of page 2, did not work.

I have one last potential solution but it is a work around that I do not want to implement, since its more of a band-aid solution and not an actual solution to an issue.

I am expecting it to fire as soon as I enter the page regardless how or when application started.

Thank you for your responses in advance.

5
  • add a delay or fire the event in postframecallback.
    – Munsif Ali
    Commented Apr 18 at 6:36
  • Provide the bloc implementation please
    – A.Ktns
    Commented Apr 18 at 7:09
  • you don't need to use StatefullWidget when using Bloc - the problem is probably with the Bloc implementation Commented Apr 18 at 11:21
  • Are you sure that the problem is with adding the event? Because i think there are more chances that, since you said you're using GetIt, you create multiple instances of BLoC, and the event is added to one instance, while you expect it in another. Can you provide the code how you create and inject mybloc(both GetIt and BlocProvider)? Commented Apr 18 at 12:39
  • had an issue with bloc implementation , thank you very much all Commented Apr 18 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.