Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'String' can't be assigned to the parameter type 'Route<Object?>'.dart(argument_type_not_assignable)

Tags:

flutter

dart

I am trying to get the name of the screen to navigate to as a string(in a custom class), when clicking a button.

class myButton extends StatelessWidget {
  String button_txt = '';
  String nextPage = '';
  double height_ = 39;

  myButton(button_txt) {
    this.button_txt = button_txt;
    this.nextPage = nextPage;
  }
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.pushReplacement(context, '/$nextPage');
      },
like image 505
Mohale Nakana Avatar asked Nov 17 '25 05:11

Mohale Nakana


1 Answers

If you want to use named routes you should use it like this:

Navigator.pushReplacementNamed(context, 'nextPage');

but if you don't want to use named routes there is different way to navigate:

  Navigator.pushReplacement(
    context,
    MaterialPageRoute(
      builder: (context) {
        return routewidget;
      },
    ),
  );
like image 142
Mahmoud Hegab Avatar answered Nov 20 '25 05:11

Mahmoud Hegab