Home > OS >  Navigate to screen without onPressed or something similar
Navigate to screen without onPressed or something similar

Time:11-30

I want to navigate from my Launcher screen to my secondScreen. I am trying to this with:

Navigator.push(context, MaterialPageRoute(builder: (context) => SomeScreen()));

I want this function to run when a specific variable in the state is modified. So I don't want it to run with an onPressed: or an onTap:

If I just call it in the build function of the Launcher widget I get the error:

Widget build(BuildContext context) {
return StoreConnector<AppState, AppState>(
        converter: (store) => store.state,
        builder: (context, state) {
            Navigator.push(
               context,
               MaterialPageRoute(builder: (context) => SomeScreen()));
setState() or markNeedsBuild() called during build.

This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#39c7e]
  state: OverlayState#759c1(entries: [OverlayEntry#f9b89(opaque: true; maintainState: false), OverlayEntry#1ff83(opaque: false; maintainState: true), OverlayEntry#5208b(opaque: false; maintainState: false), OverlayEntry#67aa9(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: StreamBuilder<AppState>
  dirty
  state: _StreamBuilderBaseState<AppState, AsyncSnapshot<AppState>>#472c9

CodePudding user response:

you can use addPostFrameCallback

 WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
Navigator.push(
               context,
               MaterialPageRoute(builder: (context) => SomeScreen()));});

not ideal

CodePudding user response:

Instead of

    converter: (store) => store.state

can't you use

    converter: (store) {
        store.state;
        Navigator.push(
           context,
           MaterialPageRoute(builder: (context) => SomeScreen()));
  • Related