Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.55k stars 392 forks source link

Inherited params not working and wrong navigation #1827

Open tomasweigenast opened 7 months ago

tomasweigenast commented 7 months ago

I have the following route hierarchy:

AdaptiveRoute(page: OrdersRouteWrapper.page, path: 'orders', children: [
            AdaptiveRoute(page: OrdersRoute.page, path: '', initial: true),
            AdaptiveRoute(
                page: OrderRoute.page,
                path: ':orderId',
                children: [AdaptiveRoute(page: OrderPaymentProofRoute.page, path: "payment-proof")]),
          ])

(it is not the root, there are other routes above /orders, and I skip them for simplicity).

First of all, I want to mention that I want to navigate to OrderPaymentProofRoute from OrdersRoute, but it will send me to OrderRoute instead of OrderPaymentProofRoute.

But I navigate first to OrderRoute, and then context.navigateTo(OrderPaymentProofRoute()); it throws the following exception:

Error [Failed to parse [String] orderId value from null]
I/flutter (12453): #0      Parameters.getString (package:auto_route/src/common/parameters.dart:61:7)
I/flutter (12453): #1      new _$AppRouter.<anonymous closure>.<anonymous closure> (package:cercanni_customers/ui/router.gr.dart:140:50)
I/flutter (12453): #2      RouteData.argsAs (package:auto_route/src/route/route_data.dart:101:22)
I/flutter (12453): #3      new _$AppRouter.<anonymous closure> (package:cercanni_customers/ui/router.gr.dart:138:30)
I/flutter (12453): #4      RootStackRouter._pageBuilder (package:auto_route/src/router/controller/root_stack_router.dart:169:20)
I/flutter (12453): #5      StackRouter._addEntry (package:auto_route/src/router/controller/routing_controller.dart:1547:29)
I/flutter (12453): #6      StackRouter._pushAllGuarded (package:auto_route/src/router/controller/routing_controller.dart:1522:29)
I/flutter (12453): <asynchronous suspension>
I/flutter (12453): 

router.gr.dart


OrderRoute.name: (routeData) {
      final pathParams = routeData.inheritedPathParams;
      final args = routeData.argsAs<OrderRouteArgs>(
          orElse: () =>
              OrderRouteArgs(orderId: pathParams.getString('orderId'))); // line 140
      return AutoRoutePage<dynamic>(
        routeData: routeData,
        child: OrderView(
          orderId: args.orderId,
          key: args.key,
        ),
      );
    },

OrderPaymentProofView:

@RoutePage()
class OrderPaymentProofView extends StatelessWidget {
  final String orderId;

  const OrderPaymentProofView({@PathParam.inherit() required this.orderId, super.key});
}

OrderView:

@RoutePage()
class OrderView extends HookWidget {
  final String orderId;

  const OrderView({@pathParam required this.orderId, super.key});
}
class OrderPaymentProofRoute extends PageRouteInfo<OrderPaymentProofRouteArgs> {
  OrderPaymentProofRoute({
    Key? key,
    List<PageRouteInfo>? children,
  }) : super(
          OrderPaymentProofRoute.name,
          args: OrderPaymentProofRouteArgs(key: key),
          initialChildren: children,
        );

  static const String name = 'OrderPaymentProofRoute';

  static const PageInfo<OrderPaymentProofRouteArgs> page =
      PageInfo<OrderPaymentProofRouteArgs>(name);
}

class OrderPaymentProofRouteArgs {
  const OrderPaymentProofRouteArgs({this.key});

  final Key? key;

  @override
  String toString() {
    return 'OrderPaymentProofRouteArgs{key: $key}';
  }
}

/// generated route for
/// [OrderView]
class OrderRoute extends PageRouteInfo<OrderRouteArgs> {
  OrderRoute({
    required String orderId,
    Key? key,
    List<PageRouteInfo>? children,
  }) : super(
          OrderRoute.name,
          args: OrderRouteArgs(
            orderId: orderId,
            key: key,
          ),
          rawPathParams: {'orderId': orderId},
          initialChildren: children,
        );

  static const String name = 'OrderRoute';

  static const PageInfo<OrderRouteArgs> page = PageInfo<OrderRouteArgs>(name);
}

class OrderRouteArgs {
  const OrderRouteArgs({
    required this.orderId,
    this.key,
  });

  final String orderId;

  final Key? key;

  @override
  String toString() {
    return 'OrderRouteArgs{orderId: $orderId, key: $key}';
  }
}

Obviously when I navigate to OrderRoute I pass the orderId parameter.

Versions:

auto_route: ^7.8.0
auto_route_generator: ^7.3.1
tomasweigenast commented 7 months ago

Is this project abandoned?

jakelaws1 commented 1 month ago

@tomasweigenast did you ever get a work around? Just pass the parameters manually instead of inheriting? Frustrating this was never addressed

tomasweigenast commented 1 month ago

I just pass the params manually