如何在Dart 3 Alpha(主分支)中启用Dart密封类

huangapple go评论77阅读模式
英文:

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'。

到目前为止,我已经采取了以下步骤:

  1. 将我的pubspec.yaml文件的环境参数设置为包含版本3.0.0。
  2. 在我的analysis_options.yaml文件中包含sealed-class语言特性。
  3. 在终端中将通道更改为master,通过设置flutter channel master
  4. 反复运行flutter cleanflutter pub cache repairflutter 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 &#39;sealed-class&#39; language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 3.0.0 or higher, and running &#39;pub get&#39;.

Steps I have taken so far:

  1. Setting my pubspec.yaml file environment parameter to be inclusive of version 3.0.0
  2. Including the sealed-class language feature in my analysis_options.yaml file.
  3. Changing channel to master with by setting flutter channel master in terminal.
  4. 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: &#39;&gt;=2.18.2 &lt;=3.0.0&#39;
...

(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: &#39;3.0.6&#39;

in my pubspec.yaml file works!

huangapple
  • 本文由 发表于 2023年3月7日 21:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定