Home > OS >  can I use bool as path parameter (PathParam) using auto route in flutter, and if so, how?
can I use bool as path parameter (PathParam) using auto route in flutter, and if so, how?

Time:11-13

I have a payment confirmation route that takes a path parameter of true or false (true when payment was successful and false when unsuccessful). my app should work on both web and mobile. I have used auto_route package for routing and I have defined my route like so:

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

and I have given the result like this to my payment page:

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()));
      }
}

However, when I navigate to the page like this:

context.router.push(PaymentConfirmationRoute(result: false));

I get this error on both web and mobile:

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

But when I manually enter the route

domain/business/campaigns/payment_result/false

in my browser, everything works fine.

I could use enums instead, but if bools don't work as pathParams, then why is there a getBool function? Am I making a mistake?

when I click on the relevent error-causing widget printed out in my console, I am directed to the router.gr file, which is generated by auto_route package. below is where I'm lead to:

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

CodePudding user response:

You need to pass String value to your path, so change your bool variable to String then inside your PaymentConfirmationPage class try convert it back to bool variable if you needed.

I think the reason it has getBool method is if you pass false as string, it will convert it for you and return bool variable to you.

  • Related