You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中Future与Navigator二次询问逻辑失效问题求助

问题解决:Flutter 多次页面跳转后的返回值处理

你的代码核心问题是第二次跳转页面的返回值没有接入到后续的回调链中,导致第二次点击"Yes"时无法触发最终的打印逻辑。下面是两种修正方案:

方案一:使用 async/await 重构(更易读)

把 onPressed 改成异步函数,用 await 依次处理两次页面跳转的返回值,逻辑更清晰:

ElevatedButton(
  child: Text('Go to the new page'),
  onPressed: () async {
    // 第一次跳转
    bool? firstAnswer = await Navigator.of(context).push<bool>(
      MaterialPageRoute(
        builder: (context) {
          return VideoScreen('Do you like the video?');
        },
      ),
    );

    if (firstAnswer == true) {
      print("He/She liked!!!");
    } else {
      // 第一次点No,跳转第二次询问
      bool? secondAnswer = await Navigator.of(context).push<bool>(
        MaterialPageRoute(
          builder: (context) {
            return VideoScreen('I wish you would liked it... Do you like the video?');
          },
        ),
      );

      // 第二次点Yes时打印目标内容
      if (secondAnswer == true) {
        print('You liked it!!!');
      }
    }
  },
)

方案二:修正原有的 then 链式调用

如果想保留 then 写法,需要把第二次跳转的 Future 返回给第一个 then 的回调,让后续的 then 能捕获到第二次页面的返回值:

ElevatedButton(
  child: Text('Go to the new page'),
  onPressed: () {
    Navigator.of(context).push<bool>(
      MaterialPageRoute(
        builder: (context) {
          return VideoScreen('Do you like the video?');
        },
      ),
    ).then((bool? answer) {
      print('answer is: ${answer}');
      if (answer == true) {
        print("He/She liked!!!");
        return Future.value(true);
      } else {
        // 返回第二次跳转的Future,让后续then能拿到它的返回值
        return Navigator.of(context).push<bool>(
          MaterialPageRoute(
            builder: (context) {
              return VideoScreen('I wish you would liked it... Do you like the video?');
            },
          ),
        );
      }
    }).then((value) {
      // 这里的value可能是第一次的true,或者第二次的返回值
      if (value == true) {
        print('You liked it!!!');
      }
    });
  },
)

关键改动说明

  • 方案一中用 await 替代 then 链,同步化异步操作逻辑,避免回调嵌套的混乱。
  • 方案二中,当第一次返回 false 时,必须返回第二次跳转的 Future,这样后续的 then 才能接收到第二个页面的返回值,否则后续 then 的 value 会是 null,无法触发打印。

另外需要确保你的 VideoScreen 页面中,点击"Yes"时返回 true,点击"No"时返回 false,示例代码如下:

class VideoScreen extends StatelessWidget {
  final String question;
  const VideoScreen(this.question, {super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Question')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(question),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () => Navigator.pop(context, true),
                  child: Text('Yes'),
                ),
                SizedBox(width: 20),
                ElevatedButton(
                  onPressed: () => Navigator.pop(context, false),
                  child: Text('No'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者emre12345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:13:40