Im trying to pass arguments thro Navigator.pushNamed but i get this error
type 'RouteSettings' is not a subtype of type 'String' in type cast
here is the Navigator
onTap: (){
Navigator.pushNamed(context, "ProductDetScreen",arguments: ProductModelsvar.id);
},
and this is where i get them in the second page
final productProviders = Provider.of<productProvider>(context);
final productId = ModalRoute.of(context)!.settings as String;
final getCurrentProduct=productProviders.findProductById(productId);
CodePudding user response:
the named routes should start with a `/'
when declaring the routes in the routes
property in MaterialApp
:
final Map<String, WidgetBuilder>? routes;
so you should declare routes like :
routes: {
"/ProductDetScreen": () => ProductDetScreen(),
// more routes
}
so you can use it inside your code like this :
Navigator.pushNamed(context, "/ProductDetScreen",arguments:
ProductModelsvar.id);
CodePudding user response:
make sure this ProductModelsvar.id
also a String
.
because you cast it as String
here:
final productId = ModalRoute.of(context)!.settings as String;
official documantation : link
but better avoid use !
for null-safety
reason. use like below:
// set empty string if its null
final productId = (ModalRoute.of(context)?.settings ?? '') as String;
ref: link