我正在Dart中试验新的空安全更新。
这是我使用的代码吗?若要初始化空变量,请执行以下操作。
void main() {
int? a;
a = null;
print('a is $a.');
}
它在Dartpad上运行良好。但是,VS代码显示以下错误
This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.10.0 or higher, and running 'pub.get'.
这个main.dart文件的主要目的是让我练习一些代码,因此根文件夹中不存在pubspec.yaml文件,因为这并不是一个完整的Flutter应用程序项目。如何查找或创建pubspec文件?
发布于 2021-05-28 13:13:54
如何查找或创建pubspec文件?
只需在项目的根目录( bin
文件夹旁边)中创建一个名为pubspec.yaml
的新文件。您可以使用以下内容:
name: my_project_name
version: 1.2.3
environment:
sdk: '>=2.12.0 <3.0.0'
这会将SDK的最低版本设置为v2.12,这将自动启用null-safety。有关详细信息,请参阅https://dart.dev/null-safety。
https://stackoverflow.com/questions/65092889
复制