How to use onGenerateRoute function to pass arguments to screens like in the examples in Flutter using Getx

Viewed 504

I want to follow the official example of how to pass arguments to named routes using the onGenerateRoute function described here: https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments#alternatively-extract-the-arguments-using-ongenerateroute

But I can't figure it out how can acomplish that using Getx, here is what i have:

Main

Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: '/list',
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/form':
          final args = settings.arguments as FormScreenArguments;

          return GetPageRoute(
            routeName: '/form',
            page: () => FormScreen(producto: args.producto),
            binding: BindingsBuilder(() {
              Get.lazyPut<FormController>(() => FormController());
            }),
          );
        }
      },
      getPages: [
        GetPage(
          name: '/list',
          page: () => const ListScreen(),
          binding: BindingsBuilder(() {
            Get.lazyPut<ListController>(() => ListController(), fenix: true);
          }),
        ),
      ],
    );
  }

The way I'm navigating to form screen:

Navigator.pushNamed(
    context,
    '/form',
    arguments: FormScreenArguments(
      producto: _px.products.elementAt(index)
    ),
);

I'm receiving this error: "Exception has occurred. _CastError (Null check operator used on a null value)"

I use print to check whats happenign, but those never enter to onGenerateRoute function.

What am I doing wrong or how should do it?

0 Answers
Related