Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The operator '[]' isn't defined for the class 'Object'. Dart

I have a Widget which on some point navigate to a different page. like:-

          Navigator.of(context).pushNamed(
              NextPage.routeName,
              arguments: {
                "tag": this.tag,
                "data": this.data,
              },
            );

now clearly Even though argument parameter accepts type Object but it also accepts Map since this sentence is not giving me an error.

And in the NextPage i am accessing the value like:-

tag: ModalRoute.of(context).settings.arguments["tag"].toString(),

Now the vscode is giving me error:-

The operator '[]' isn't defined for the class 'Object'.
Try defining the operator '[]'.dart(undefined_operator)

I don't know why vscode is giving me an error. So, Either the Object should have the [] or Map should be a type of Object.

Or there is something about the Date which is not clear.

Note: data is object.

How do I remove this error?

like image 338
rahul Kushwaha Avatar asked Feb 16 '20 06:02

rahul Kushwaha


2 Answers

ModalRoute.settings.arguments is a property with the type Object. You cannot call an indexer [] on an Object. Everything in Dart inherits from Object, which is why you can pass your arguments to the ModalRoute no matter what it is. In order to use it, though, you need to first cast it to the type you are expecting to work with.

tag: (ModalRoute.of(context).settings.arguments as Map)["tag"].toString(),
like image 99
Abion47 Avatar answered Oct 26 '22 18:10

Abion47


ModalRoute.settings.arguments is a property with the type Object.so you can make it Map

final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;

final tag = routeArgs['tag'];
like image 28
Mahmoud Avatar answered Oct 26 '22 18:10

Mahmoud