Joe*_*app 3 typechecking dart flutter
我习惯于依靠编译器来捕获不兼容的类型错误。默认情况下,Dart 仅在我记得指定类型时才提供此功能。如果我忘记在代码中包含类型,那么我不会得到类型检查。
如何让 Flutter/Dart 编译器在以下代码中的指示行上出错?尽管充满了类型错误,但这段代码编译得很好。
class Foo {
String foo() {
return "foo";
}
}
class Bar {
String bar() {
return "bar";
}
}
f() { // would like a missing return type error here (e.g. void)
print("returns nothing");
}
void g(x) { // would like a missing parameter type error here...
print(x.bar); // ...so this isn't a missing property at runtime
}
void h() {
String a = f(); // would like this to error...
print("$a"); // ...instead of this printing "null"
g(Foo()); // would like this to error for incorrect parameter type
}
Run Code Online (Sandbox Code Playgroud)
如果有办法做到这一点,我该如何在 Visual Studio Code 和 Intellij/Android Studio 以及 dart2js 中做到这一点?
Rém*_*let 10
您可以使用该analysis_options.yaml文件使 Dart 更严格。
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
Run Code Online (Sandbox Code Playgroud)
将此文件放在项目的根目录中。
这将禁用 Dart 回退的情况dynamic并使其成为编译错误。
它还禁用隐式向上转换,如下所示:
Object a = 42;
String b = a; // unless `implicit-cast` is disabled, this is valid
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |