flutter Dart中两个变量同名不同作用域时如何指定目标变量?

cu6pst1q  于 2022-12-05  发布在  Flutter
关注(0)|答案(1)|浏览(134)

假设在Dart(DartPad)中,我们有:

String a = "I am higher in the scope chain";

void main() {
  String a = "I am lower in the scope chain, but I have the same name";
  print(a);
}

是否有一个关键字可以用来打印父作用域中的变量,例如:

print(parent.a)

print(this.a)


(这些不起作用)。

23c0lvtd

23c0lvtd1#

如果您的文件名是'some_dart_file. dart',则可以执行此操作

import './some_dart_file.dart' as parent show a;

String a = "I am higher in the scope chain";

void main() {
  String a = "I am lower in the scope chain, but I have the same name";
  print(parent.a);
}

相关问题