在没有 onPressed 或类似内容的情况下导航到屏幕

Navigate to screen without onPressed or something similar

我想从我的 Launcher 屏幕导航到我的 secondScreen。我正在尝试这样做:

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

我希望这个函数在state中的特定变量被修改时运行。所以我不希望它 运行 与 onPressed:onTap:

如果我只是在 Launcher 小部件的 build 函数中调用它,我会收到错误消息:

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

你可以使用addPostFrameCallback

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

不理想

而不是

    converter: (store) => store.state

你不能用

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