当我尝试使用required关键字时,出现了“Undefined class 'required'”错误。

8

我遇到了错误:使用required关键字时出现未定义的类“required”。

以下是代码示例:

class Field {
  final int id;
  final String name;
  final bool userEditable;
  final String title;
  Field({
    required this.id,
    this.name,
    this.userEditable,
    this.title,
  });
}

enter image description here

我已经在使用Flutter的@required关键字,但我正在尝试切换到Dart的新required关键字。
这是pubspec.yaml的环境SDK行:
environment:
  sdk: ">=2.7.0 <3.0.0"

这是Flutter --version的输出:

Flutter 2.0.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision c5a4b4029c (2 weeks ago) • 2021-03-04 09:47:48 -0800
Engine • revision 40441def69
Tools • Dart 2.12.0

3
你不能仅仅凭借兴致就使用新的语言特性。将你的最小SDK版本限制更新为 2.12.0 - Christopher Moore
哦,我没注意到。谢谢。如果你能把它作为答案添加进去,我会点赞的。 - ARK4579
1个回答

18

将您的最低sdk限制更新为2.12.0

environment:
  sdk: ">=2.12.0 <3.0.0"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接