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

Flutter AutoRoute能否用bool作路径参数?报错如何解决?

关于auto_route中bool类型路径参数的导航报错问题

我需要实现一个支付确认路由,接收表示支付成功(true)或失败(false)的bool类型路径参数,应用需同时适配Web与移动端。目前使用auto_route包定义路由如下:

AutoRoute(
  path: '/business/campaigns/payment_result/:result',
  page: PaymentConfirmationPage,
  guards: [AuthenticationRouteGuard, BusinessAccessRouteGuard],
),

在PaymentConfirmationPage中通过@PathParam()接收参数:

class PaymentConfirmationPage extends StatelessWidget {
       const PaymentConfirmationPage( {
          @PathParam() required bool result,
           Key? key})
         : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        final bool result = context.router.current.pathParams.getBool('result');
        return Container(child: Text(result.toString()));
      }
}

但通过context.router.push(PaymentConfirmationRoute(result: false));进行导航时,Web与移动端均出现报错:

Class 'bool' has no instance method 'toLowerCase'. Receiver: false
Tried calling: toLowerCase()

手动在浏览器输入路由domain/business/campaigns/payment_result/false却能正常运行。我知道可以改用枚举来实现,但既然auto_route提供了getBool方法,说明bool类型应该支持作为PathParam,想确认是否是我的操作存在错误。报错指向auto_route生成的router.gr文件中的这段代码:

return _i83.MaterialPageX<dynamic>(
    routeData: routeData,
    child: _i39.PaymentConfirmationPage(
      result: args.result,
      key: args.key,
    ),
  );

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 11:15:35