Home > Back-end >  Argument not being passed to screen
Argument not being passed to screen

Time:05-02

In main.dart created such routes:

routes: {
          '/home': (context) => HomeScreenWidget(),
          '/color_picker': (context) => ColorPickerWidget(),
          TextValueScreen.routeName: (context) =>
          const TextValueScreen(color: '',),
        },
        initialRoute: '/home',

I'm trying to pass an argument "color" to the following screen:

// default value of select
  String selectedValue = "Yellow";

  // next step route
  void _changeColor(String color) {
    Navigator.pushNamed(
      context,
      TextValueScreen.routeName,
      arguments: {color: color}
    );
  }

ElevatedButton(onPressed: () => _changeColor(selectedValue),
                child: const Text('Next'),
            ),

But the argument is not passed. How can this problem be solved?

CodePudding user response:

This is how to access arguments on a target route:

final args = ModalRoute.of(context)!.settings.arguments

CodePudding user response:

Access the Navigation argument inside the build method of your next screen like this var arg = ModalRoute.of(context)!.settings.arguments as Color;
Check if the argument you are passing is indeed a Color

  • Related