没有为类“Object"定义运算符“[]".镖 [英] The operator '[]' isn't defined for the class 'Object'. Dart
问题描述
我有一个Widget,该Widget有时会导航到其他页面.像:-
Navigator.of(context).pushNamed(NextPage.routeName,参数:{标签":this.tag,数据":this.data,},);
现在很清楚,即使 argument
参数接受类型为 Object
的类型,但它也接受 Map
的类型,因为这句话并没有给我带来错误.>
在 NextPage
中,我正在访问类似以下值:-
标签:ModalRoute.of(context).settings.arguments ["tag"].toString(),
现在,vscode给了我错误:-
未为类"Object"定义运算符"[]".尝试定义运算符'[]'.dart(undefined_operator)
我不知道为什么 vscode
给我一个错误.因此, Object
应该具有 []
或 Map
应该是Object的类型.
或者关于日期还有一些不清楚的地方.
注意:数据
是对象.
如何清除此错误?
<代码> ModalRoute.settings.arguments 是类型为 Object
的属性.您不能在 Object
上调用索引器 []
.Dart中的所有内容都继承自 Object
,这就是为什么无论参数如何都可以将参数传递给 ModalRoute
的原因.但是,要使用它,您需要首先将其转换为期望使用的类型.
<代码>标签:(作为Map的ModalRoute.of(context).settings.arguments)["tag"].toString(),
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?
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(),
这篇关于没有为类“Object"定义运算符“[]".镖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!