Dart Null-Safety - 执行 dart 迁移以升级到新的 null 安全性时出现范围错误
Posted
技术标签:
【中文标题】Dart Null-Safety - 执行 dart 迁移以升级到新的 null 安全性时出现范围错误【英文标题】:Dart Null-Safety - Range Error when executing dart migrate to upgrade to new null safety 【发布时间】:2021-10-18 14:57:46 【问题描述】:我在将自定义包升级到 null 安全时遇到问题,并且在此处和任何其他平台上搜索时找不到任何解决方案,甚至没有找到相同的问题。
在为 Flutter 2.0 空安全升级自定义包时,我按照指南的建议使用 dart migrate
,但出现编译器错误。
但是错误没有指定它发生的位置,错误描述也没有任何帮助。我查看了整个包以发现任何 Array/List 或包含范围错误,但没有任何成功。
我没有办法修复此错误并完成对 null 安全的升级。有人知道我可以尝试解决此错误吗?
错误信息:
dart migrate
Migrating file_name
See https://dart.dev/go/null-safety-migration for a migration guide.
Note: more than one project found; migrating the top-level project.
Analyzing project...
[--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]No analysis issues found.
Generating migration suggestions...
[--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]
Compiling instrumentation information...
[--------------------| ]RangeError (index): Invalid value: Not in inclusive range 0..12: 13
#0 List.[] (dart:core-patch/array.dart:268:52)
#1 BestPracticesVerifier._targetKindsFor (package:analyzer/src/error/best_practices_verifier.dart:1582:38)
#2 BestPracticesVerifier.visitAnnotation (package:analyzer/src/error/best_practices_verifier.dart:250:17)
#3 AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:280:49)
#4 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
#5 AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:148:17)
#6 ClassDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1559:11)
#7 RecursiveAstVisitor.visitClassDeclaration (package:analyzer/dart/ast/visitor.dart:707:10)
#8 BestPracticesVerifier.visitClassDeclaration (package:analyzer/src/error/best_practices_verifier.dart:320:13)
#9 ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:1536:49)
#10 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
#11 CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2141:21)
#12 RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:731:10)
#13 CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
#14 LibraryAnalyzer._computeHints (package:analyzer/src/dart/analysis/library_analyzer.dart:281:10)
#15 LibraryAnalyzer.analyzeSync.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:167:9)
#16 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
#17 LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:166:13)
#18 LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:107:12)
#19 AnalysisDriver._computeResolvedLibrary2.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1416:65)
#20 PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#21 AnalysisDriver._computeResolvedLibrary2 (package:analyzer/src/dart/analysis/driver.dart:1402:20)
#22 AnalysisDriver._computeResolvedLibrary.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1395:15)
#23 _rootRun (dart:async/zone.dart:1354:13)
#24 _CustomZone.run (dart:async/zone.dart:1258:19)
#25 _runZoned (dart:async/zone.dart:1788:10)
#26 runZoned (dart:async/zone.dart:1710:10)
#27 NullSafetyUnderstandingFlag.enableNullSafetyTypes (package:analyzer/dart/element/null_safety_understanding_flag.dart:42:12)
#28 AnalysisDriver._computeResolvedLibrary (package:analyzer/src/dart/analysis/driver.dart:1394:40)
#29 AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:964:22)
#30 AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2018:24)
<asynchronous suspension>
【问题讨论】:
【参考方案1】:这似乎是 Dart SDK 本身的一个错误。这在dart-lang/sdk#46183 中进行了跟踪,并在this commit 中进行了修复。
试一试的步骤:
-
运行
flutter upgrade
以确保您使用的是最新版本。
如果之后仍然遇到问题,请考虑切换到 Flutter 的 beta
频道以获得更先进的 SDK 以运行迁移工具,然后切换回 stable
频道完毕。为此,您可以运行 flutter channel beta
,然后重新运行迁移,然后 flutter channel stable
切换回来。
【讨论】:
【参考方案2】:我遇到了同样的问题,如 cbracken 的回答中所述,它已在 beta 通道中修复。
另外,就我而言,我在 Flutter 外部安装了第二个 Dart SDK (2.12),所以问题似乎没有解决。
确保您在 Flutter SDK Flutter/bin
文件夹中使用 dart included 运行 dart migrate
。
【讨论】:
以上是关于Dart Null-Safety - 执行 dart 迁移以升级到新的 null 安全性时出现范围错误的主要内容,如果未能解决你的问题,请参考以下文章