dart-migration-versioning
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart Version Management and Migration
Dart版本管理与迁移
Goal
目标
Manages Dart language versioning, resolves breaking changes during SDK upgrades, and applies per-library version overrides to facilitate gradual migrations. Assumes a standard Dart or Flutter environment with access to and source files. Related Skills: , .
pubspec.yamldart-web-developmentdart-static-analysis管理Dart语言版本控制,解决SDK升级过程中的破坏性变更问题,应用库级别的版本覆盖以支持渐进式迁移。假设使用标准Dart或Flutter环境,可访问和源文件。相关技能:、。
pubspec.yamldart-web-developmentdart-static-analysisDecision Logic
决策逻辑
When tasked with upgrading a Dart project or fixing version-related compilation errors, follow this evaluation path:
- Identify Target SDK: Determine the target Dart SDK version.
- Check Current Version: Read to find the current lower bound SDK constraint.
pubspec.yaml - Evaluate Breaking Changes: Cross-reference the version jump with the breaking changes log.
- If migrating to >= 3.0.0: Sound null safety is strictly enforced.
- If migrating web libraries to >= 3.3.0: Legacy and
dart:htmlare deprecated/removed.dart:js
- Determine Migration Strategy:
- Can the code be refactored immediately? -> Apply API migrations.
- Is the refactor too large or blocked? -> Pin specific files using to maintain older language semantics while upgrading the package.
@dart = <version>
当接到升级Dart项目或修复版本相关编译错误的任务时,遵循以下评估路径:
- 确定目标SDK: 明确目标Dart SDK版本。
- 检查当前版本: 读取找到当前的SDK约束下限。
pubspec.yaml - 评估破坏性变更: 对照版本跨度对应的破坏性变更日志进行核对。
- 如果升级到 >= 3.0.0版本: 严格强制启用Sound null safety机制。
- 如果将Web库升级到 >= 3.3.0版本: 旧版和
dart:html已弃用/移除。dart:js
- 确定迁移策略:
- 代码是否可以立即重构? -> 执行API迁移。
- 重构工作量过大或存在阻塞? -> 使用标注指定文件,在升级包的同时保留旧版语言语义。
@dart = <version>
Instructions
操作指南
-
Verify Current Language Version Requirements Inspect thefile to determine the default language version, which is dictated by the lower bound of the SDK constraint.
pubspec.yamlyamlenvironment: sdk: '>=2.18.0 <3.0.0' # Defaults to Dart 2.18 -
Consult Breaking Change Logs Before performing major dependency or SDK upgrades, review the breaking changes for the target version. STOP AND ASK THE USER: "I am about to upgrade the SDK constraint to. This includes breaking changes such as
<target_version>. Should I proceed with the upgrade and begin refactoring?"<list_key_changes> -
Apply Per-Library Language Version Selection (Gradual Migration) If a specific file cannot be immediately migrated to the new language version (e.g., pending null safety migration or complex refactoring), pin the file to an older version.
- The string must be in a
@dartcomment.// - It must appear before any Dart code in the file.
dart// @dart = 2.19 import 'dart:math'; // Legacy code that relies on 2.19 semantics void legacyFunction() { // ... } - The
-
Migrate Deprecated Web and JS Interop Libraries (Dart 3.3+) If the project targets Dart 3.3+ or compiles to Wasm, legacy web libraries (,
dart:html,dart:js) will cause compilation errors. Migrate these topackage:jsandpackage:web.dart:js_interopdart// BEFORE (Legacy) import 'dart:html'; import 'package:js/js.dart'; () external void legacyJsFunction(); // AFTER (Modern) import 'package:web/web.dart' as web; import 'dart:js_interop'; () external void modernJsFunction(); void updateDom() { final div = web.document.createElement('div') as web.HTMLDivElement; div.text = 'Migrated'; web.document.body?.append(div); } -
Validate-and-Fix Loop After updating the SDK constraint or modifying code, run static analysis to verify the migration.bash
dart analyzeFixing common post-upgrade errors:- If or
unnecessary_non_null_assertionappears, remove theinvalid_null_aware_operatoror!as type promotion has likely improved in the newer Dart version.?. - If or
dart:js_utilthrows Wasm compilation errors, return to Step 4.dart:html
- If
-
核对当前语言版本要求 检查文件确定默认语言版本,该版本由SDK约束的下限决定。
pubspec.yamlyamlenvironment: sdk: '>=2.18.0 <3.0.0' # 默认使用Dart 2.18 -
查阅破坏性变更日志 在执行重大依赖或SDK升级前,查看目标版本的破坏性变更说明。 请先询问用户: "我即将把SDK约束升级到,本次升级包含以下破坏性变更:
<target_version>。请问我是否可以继续升级并开始重构代码?"<list_key_changes> -
应用库级语言版本选择(渐进式迁移) 如果特定文件无法立即适配新的语言版本(例如待处理空安全迁移或复杂重构),可将该文件固定到旧版本。
- 字符串必须放在
@dart注释中。// - 必须出现在文件内所有Dart代码之前。
dart// @dart = 2.19 import 'dart:math'; // 依赖2.19版本语义的旧版代码 void legacyFunction() { // ... } -
迁移已弃用的Web和JS互操作库(Dart 3.3+) 如果项目目标版本为Dart 3.3+或需要编译为Wasm,旧版Web库(、
dart:html、dart:js)会引发编译错误,请将其迁移到package:js和package:web。dart:js_interopdart// 改造前(旧版) import 'dart:html'; import 'package:js/js.dart'; () external void legacyJsFunction(); // 改造后(新版) import 'package:web/web.dart' as web; import 'dart:js_interop'; () external void modernJsFunction(); void updateDom() { final div = web.document.createElement('div') as web.HTMLDivElement; div.text = 'Migrated'; web.document.body?.append(div); } -
验证与修复循环 更新SDK约束或修改代码后,运行静态分析验证迁移结果。bash
dart analyze修复升级后常见错误:- 如果出现或
unnecessary_non_null_assertion报错,移除invalid_null_aware_operator或!即可,因为新版Dart的类型推导能力大概率已经得到优化。?. - 如果或
dart:js_util抛出Wasm编译错误,回到步骤4处理。dart:html
- 如果出现
Constraints
约束规则
- DO check current language version requirements in before making any syntax changes.
pubspec.yaml - DO use to pin specific files to older versions during migration if a full refactor is not requested.
@dart = <version> - DO consult breaking change logs before performing major dependency upgrades.
- NEVER use or
///for the/* */version override comment; it must be@dart.// - NEVER place the comment after imports or code declarations.
@dart - NEVER attempt to use ,
dart:html, ordart:jswhen compiling to WebAssembly (dart:js_util).dart compile wasm - NEVER disable sound null safety in Dart 3.0+; the flag and unsound execution are completely removed.
--no-sound-null-safety
- 进行任何语法变更前,必须先检查中的当前语言版本要求。
pubspec.yaml - 如果未要求全量重构,迁移过程中必须使用将特定文件固定到旧版本。
@dart = <version> - 执行重大依赖升级前必须查阅破坏性变更日志。
- 禁止使用或
///书写/* */版本覆盖注释,必须使用@dart。// - 禁止将注释放在导入语句或代码声明之后。
@dart - 编译为WebAssembly()时禁止使用
dart compile wasm、dart:html或dart:js。dart:js_util - Dart 3.0+版本中禁止禁用Sound null safety;参数和非健全执行模式已被完全移除。
--no-sound-null-safety