我有一个小部件,它在某些时候导航到不同的页面。
喜欢:-

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

现在清楚即使 argument 参数接受类型 Object 但它也接受 Map 因为这句话没有给我一个错误。

NextPage 中,我访问的值如下:-
tag: ModalRoute.of(context).settings.arguments["tag"].toString(),

现在 vscode 给了我错误:-
The operator '[]' isn't defined for the class 'Object'.
Try defining the operator '[]'.dart(undefined_operator)

我不知道为什么 vscode 给我一个错误。
所以,要么 Object 应该有 [] 要么 Map 应该是一种对象。

或者有一些关于日期的不清楚。

注意: data 是对象。

如何消除此错误?

最佳答案

ModalRoute.settings.arguments Object 类型的属性。您不能在 [] 上调用索引器 Object 。 Dart 中的所有内容都继承自 Object ,这就是为什么您可以将参数传递给 ModalRoute 无论它是什么。但是,为了使用它,您需要首先将它转换为您希望使用的类型。

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

关于dictionary - 操作符 '[]' 不是为类 'Object' 定义的。 Dart ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60245865/

10-12 07:33
查看更多