'Flutter how I will grab argument int value from map object

I am new in dart and flutter

I have used pushNamed to send category id to category_details page

onTap: () {
        Navigator.of(context).pushNamed(
          CategoryDetailsPage.routeName, 
          arguments: <String, int>{'catId': id},
    );
},

In category details page build method I have tried to receive this argument like below way

final categoryId = ModalRoute.of(context)!.settings.arguments;

After print, in console I got the value as a MAP

flutter: {catId: 1}

So I tried to access the catId value like

int id = categoryId!["catId"];

I'm getting below error,

The operator '[]' isn't defined for the type 'Object'.
Try defining the operator '[]'.

How can I grab this id for continue my next integration ?



Solution 1:[1]

try casting the object to a map like this:

final Map<String, int> categoryId = ModalRoute.of(context)!.settings.arguments as Map<String, int>;

int? id = categoryId!["catId"];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Er1