type 'String' is not a subtype of type 'Map<String, String>' in type cast

Issue

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)

Solution

Demo


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false, routes: {
      '/': (context) => CategoriesScreen(), // the HomePage
      CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),
    });
  }
}

class CategoriesScreen extends StatefulWidget {
  CategoriesScreen({Key? key}) : super(key: key);

  @override
  _MyWidgetXState createState() => _MyWidgetXState();
}


class _MyWidgetXState extends State<CategoriesScreen> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {
              Navigator.of(context)
                  .pushNamed(CategoryMealsScreen.routeName, arguments: {
                'id': "id 1",
                'title': "title",
              });
            },
            child: Text("pop"),
          ),
        ],
      ),
    );
  }
}

class CategoryMealsScreen extends StatefulWidget {
  static final String routeName = "/asdasd";
  CategoryMealsScreen({Key? key}) : super(key: key);

  @override
  _MyW2State createState() => _MyW2State();
}

class _MyW2State extends State<CategoryMealsScreen> {
  @override
  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"];

    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: Text("pop, $categoryTitle $categoryId"),
        ),
      ),
    );
  }
}

Answered By – Yeasin Sheikh

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.