我正在 Flutter 项目中试用 MobX,我引用了一个示例,但我升级了版本,这样我就可以练习使用 null 安全性进行编码。在我运行 pub upgrade 之前它一直在工作。运行 build_runner 开始给我这个错误:
D:\projects\flutter_mobx_base_code>flutter pub run build_runner clean
Failed to precompile build_runner:build_runner:
/D:/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:1600:40: Error: Getter not found: 'topLevelVariable'.
return kinds.contains(TargetKind.topLevelVariable);
^^^^^^^^^^^^^^^^
/D:/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Getter not found: 'topLevelVariable'.
case TargetKind.topLevelVariable:
^^^^^^^^^^^^^^^^
/D:/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Type 'dynamic' of the case expression is not a subtype
of type 'TargetKind' of this switch expression.
- 'TargetKind' is from 'package:meta/meta_meta.dart' ('/D:/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.3.0/lib/meta_meta.dart').
case TargetKind.topLevelVariable:
^
/D:/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2001:13: Context: The switch expression is here.
switch (this) {
^
这些是我的依赖项:
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
mobx: ^2.0.0
flutter_mobx: ^2.0.0
mobx_codegen: ^2.0.0
provider: 5.0.0
dev_dependencies:
build_runner: ^1.12.2
flutter_test:
sdk: flutter
但是,如果我使用以下版本, build_runner 会按预期工作:
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
mobx: ^0.1.2
flutter_mobx: ^0.1.1
mobx_codegen: ^0.1.0+1
provider: ^1.6.1
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.3.0
我真的很想实现空安全,但我不能用这个错误。我无法理解此错误以及它的来源,因此我无法在此错误来自的任何包中发布问题。我的依赖项设置有问题吗?
我尝试过flutter clean -> flutter pub cache repair -> flutter pub get
,在一个新项目中重新开始,以及我认为可以解决问题的任何事情,我放弃了,这就是我在这里的原因。任何建议和帮助将不胜感激!谢谢!