10

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?

2 Answers 2

49

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(),
4
  • how does this work for you with casting the arguments which is type _InternalLinkedHashMap<String, String> (the mentioned Object) into Map? I can't get this example to work. What Dart SDK version you have used? Is it possible to pass arguments not as simple type like String, but custom class instance ?
    – magnump0
    Commented Mar 10, 2021 at 18:15
  • @boldnik I'm not sure what you mean, but if the cast from Object to Map isn't working for you, then your arguments object must not actually be a Map. This isn't a magical conversion; this cast only works because in OP's case, arguments was a Map that was hidden by the type label Object. Casting only changes the apparent type a value from one compatible type to another - it doesn't change its actual underlying type. Try running ModalRoute.of(context).settings.arguments.runtimeType and if that value isn't some version of Map, casting to Map will fail.
    – Abion47
    Commented Mar 10, 2021 at 20:09
  • This is extremely unsafe, how can we define the type in the route definitions so that we do not have to unsafely assert? Commented Oct 11, 2023 at 7:47
  • @Karatekid430 In vanilla Flutter, you can't with named routes as the abstraction between the name and the widget discards the typing information. If you're concerned about the lack of type safety, you will need to manually check the type of arguments before attempting to cast it. If this is a dealbreaker, some other options are to use a navigation plugin like go_router or auto_route that offers statically typed routing or eschew navigation arguments altogether in favor of a state-management-based solution.
    – Abion47
    Commented Oct 11, 2023 at 14:19
1

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'];
1
  • Hey @Mahmuoud! Try to post an answer if it adds to the current discussion and only if your solution is different than the others posted earlier.
    – happy-san
    Commented Jul 22, 2021 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.