没有为类“Object"定义运算符“[]".镖 [英] The operator '[]' isn't defined for the class 'Object'. Dart

查看:71
本文介绍了没有为类“Object"定义运算符“[]".镖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆