英文:
How to Enable Dart Sealed Classes in Dart 3 Alpha (master branch)
问题
我试图在Dart Master分支中测试封闭类,但我一直遇到这个错误(在VSCode的sealed
关键字上显示为一个lint错误):
这需要启用 'sealed-class' 语言功能。
尝试更新您的pubspec.yaml以将最低SDK约束设置为3.0.0或更高,并运行 'pub get'。
到目前为止,我已经采取了以下步骤:
- 将我的
pubspec.yaml
文件的环境参数设置为包含版本3.0.0。 - 在我的
analysis_options.yaml
文件中包含sealed-class
语言特性。 - 在终端中将通道更改为
master
,通过设置flutter channel master
。 - 反复运行
flutter clean
、flutter pub cache repair
、flutter upgrade
。
是否有其他人能够使这个工作?以下是相关文件的一些片段。
pubspec.yaml:
...
# 产品和文件版本的前缀,而build-number用作构建后缀。
version: 1.0.0+1
environment:
sdk: '>=2.18.2 <=3.0.0'
...
(注意:在此参数中,我已尝试各种配置:'3.0.0','3.0.0-290.0.dev','>=2.18.2'。第一个不允许,其他的都导致了相同的问题。)
我的analysis_options.yaml文件包括以下内容:
...
include: package:flutter_lints/flutter.yaml
analyzer:
enable-experiment:
- records
- patterns
- sealed-class
linter:
...
这是我通过flutter --version
获得的信息:
Flutter 3.9.0-1.0.pre.55 • 通道 master • https://github.com/flutter/flutter.git
Framework • 修订版本 d696b05165(26小时前) • 2023-03-06 11:41:59 +0000
引擎 • 修订版本 197b6dae1c
工具 • Dart 3.0.0(构建 3.0.0-290.0.dev) • DevTools 2.22.2
希望这能帮助解决你的问题。
英文:
I'm trying to test out sealed classes in the Dart Master branch, but I keep running into this error (it is showing up as a lint error over the sealed
keyword in VSCode):
This requires the 'sealed-class' language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 3.0.0 or higher, and running 'pub get'.
Steps I have taken so far:
- Setting my
pubspec.yaml
file environment parameter to be inclusive of version 3.0.0 - Including the
sealed-class
language feature in myanalysis_options.yaml
file. - Changing channel to master with by setting
flutter channel master
in terminal. - Repeatedly running
flutter clean
,flutter pub cache repair
,flutter upgrade
.
Has anyone else been able to get this to work? Below are some snippets from the relevant files.
pubspec.yaml:
...
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1
environment:
sdk: '>=2.18.2 <=3.0.0'
...
(NOTE: On this one, I've tried various configurations in this parameter: '3.0.0', '3.0.0-290.0.dev', '>=2.18.2'. The first was not allowed, the others led to the same issue.)
My analysis_options.yaml file has the following:
...
include: package:flutter_lints/flutter.yaml
analyzer:
enable-experiment:
- records
- patterns
- sealed-class
linter:
...
And here's what I get with flutter --version
:
Flutter 3.9.0-1.0.pre.55 • channel master • https://github.com/flutter/flutter.git
Framework • revision d696b05165 (26 hours ago) • 2023-03-06 11:41:59 +0000
Engine • revision 197b6dae1c
Tools • Dart 3.0.0 (build 3.0.0-290.0.dev) • DevTools 2.22.2
答案1
得分: 0
我不知道是什么,但在某个时候我确实更新了VSCode,然后似乎在 linter 中工作。但是,当我想在运行应用程序时使用它时,必须像 @Olga P 和 @mmcdon20 所提到的那样在命令行中使用实验性标志。
英文:
I don't know what it was but I did get an update to VSCode at some point and then it seemed to be working in the linter. But then had to use the experimental flag in command line as mentioned by @Olga P and @mmcdon20 when I wanted to use it in running the app.
答案2
得分: 0
对我来说,在我的 pubspec.yaml 文件中更改以下部分:
environment:
sdk: '3.0.6'
英文:
For me, changing
environment:
sdk: '3.0.6'
in my pubspec.yaml file works!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论