Home > Back-end >  Flutter how I will grab argument int value from map object
Flutter how I will grab argument int value from map object

Time:03-12

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 ?

CodePudding user response:

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"];
  • Related