Home > database >  Navigator.of(context).pushAndRemoveUntil is erroring out saying I cant pass it a string
Navigator.of(context).pushAndRemoveUntil is erroring out saying I cant pass it a string

Time:07-29

I have been following this tutorial and I am at this point and am receiving this error:

The argument type 'String' can't be assigned to the parameter type 'Route<Object?

I have built my routes within my main function in my main.dart file:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
      routes: {
        //define routes in this map
        '/login/': (context) => const LoginView(),
        '/register/': (context) => const RegisterView(),
      },
    ),
  );
}

And then in my login_view.dart file I try and use this route like this:

    TextButton(
      onPressed: () {
        Navigator.of(context)
            .pushAndRemoveUntil('/register/', (route) => false);
      },
      child: const Text('Register'),
    )

The error I mentioned above is at the '/register/' which seems odd. I says it can not be a type string but in the tutorial he writes a string like this, and its this string that is in the map that defines the route... Not sure what I am missing here.

CodePudding user response:

You used pushAndRemoveUntil which requires Route<Object but as you are using Named routes u need to use pushNamedAndRemoveUntil

TextButton(
  onPressed: () {
    Navigator.of(context)
        .pushNamedAndRemoveUntil('/register/', (route) => false);
  },
  child: const Text('Register'),
)
  • Related