when I click on a button in my Flutter App I got an error anyone here has a solution?!
The routes
routes:{
'/':(context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),}
The arguments
void selectCategory(BuildContext ctx) {
Navigator.of(ctx).pushNamed(CategoryMealsScreen.routeName,
arguments: {
'id': id,
'title': title,
});
and the map
Widget build(BuildContext context) {
final routeArg = ModalRoute.of(context)?.settings.arguments as Map<String, String>; // the question mark is needed but I don't know why..!
final categoryId = routeArg["id"];
final categoryTitle = routeArg["title"];
final categoryMeals = DUMMY_MEALS.where((meal){
return meal.categories.contains(categoryId);
}).toList();
The full error
The following _CastError was thrown building CategoryMealsScreen(dirty, dependencies: [_ModalScopeStatus], state: _CategoryMealsScreenState#b3c64): type 'String' is not a subtype of type 'Map<String, String>' in type cast
The relevant error-causing widget was: CategoryMealsScreen file:///C:/Users/DELL/AndroidStudioProjects/meal_app/lib/main.dart:37:50 When the exception was thrown, this was the stack: #0 _CategoryMealsScreenState.build (package:meal_app/screens/category_meals_screen.dart:16:65) #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) #3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) #4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)