概述

#

根据 Flutter 的弃用策略,在 1.22 稳定版发布后达到生命周期结束的已弃用 API 已被移除。这是 Flutter 首次移除已弃用 API,其中一些弃用早于我们的迁移指南策略。

所有受影响的 API 已汇编到此主要来源中,以帮助迁移。还提供了快速参考表

有关 Flutter 弃用策略的更多背景信息,请参阅设计文档文章

变更

#

本节列出了按受影响类别的弃用内容。

CupertinoDialog

#

受修复工具支持:仅限 IDE 修复。

CupertinoDialog 在 v0.2.3 中已弃用。请改用 CupertinoAlertDialogCupertinoPopupSurface

迁移指南

CupertinoAlertDialog

迁移前的代码

dart
CupertinoDialog(child: myWidget);

迁移后的代码

dart
CupertinoAlertDialog(content: myWidget);

CupertinoPopupSurface

迁移前的代码

dart
CupertinoDialog(child: myWidget);

迁移后的代码

dart
CupertinoPopupSurface(child: myWidget);

参考资料

API 文档

相关问题

相关 PR


Cupertino 导航栏的 actionsForegroundColor

#

受修复工具支持:否

CupertinoNavigationBar.actionsForegroundColorCupertinoSliverNavigationBar.actionsForegroundColor 在 v1.1.2 中已弃用。请改为在 CupertinoTheme 中设置 primaryColor 来传播此属性。要访问 primaryColor,请调用 CupertinoTheme.of(context).primaryColor

迁移指南

迁移前的代码

dart
CupertinoNavigationBar(
  actionsForegroundColor: CupertinoColors.systemBlue,
);
CupertinoSliverNavigationBar(
  actionsForegroundColor: CupertinoColors.systemBlue,
);

迁移后的代码

dart
CupertinoTheme(
  data: CupertinoThemeData(
    primaryColor: CupertinoColors.systemBlue
  ),
  child: ...
);

// To access the color from the `CupertinoTheme`
CupertinoTheme.of(context).primaryColor;

参考资料

API 文档

相关问题

相关 PR


CupertinoTextThemeData.brightness

#

受修复工具支持:是

CupertinoTextThemeData.brightness 在 v1.10.14 中已弃用。此字段成员在弃用时已失效。此参数没有替代项,应移除所有引用。

迁移指南

迁移前的代码

dart
const CupertinoTextThemeData themeData = CupertinoTextThemeData(brightness: Brightness.dark);
themeData.copyWith(brightness: Brightness.light);

迁移后的代码

dart
const CupertinoTextThemeData themeData = CupertinoTextThemeData();
themeData.copyWith();

参考资料

API 文档

相关问题

相关 PR


fromHoverEvent 构造的指针事件

#

受修复工具支持:是

PointerEnterEventPointerExitEventfromHoverEvent 构造函数在 v1.4.3 中已弃用。应改用 fromMouseEvent 构造函数。

迁移指南

迁移前的代码

dart
final PointerEnterEvent enterEvent = PointerEnterEvent.fromHoverEvent(PointerHoverEvent());
final PointerExitEvent exitEvent = PointerExitEvent.fromHoverEvent(PointerHoverEvent());

迁移后的代码

dart
final PointerEnterEvent enterEvent = PointerEnterEvent.fromMouseEvent(PointerHoverEvent());
final PointerExitEvent exitEvent = PointerExitEvent.fromMouseEvent(PointerHoverEvent());

参考资料

API 文档

相关问题

相关 PR


showDialog 使用 builder

#

受修复工具支持:是

showDialogchild 参数在 v0.2.3 中已弃用。应改用 builder 参数。

迁移指南

迁移前的代码

dart
showDialog(child: myWidget);

迁移后的代码

dart
showDialog(builder: (context) => myWidget);

参考资料

API 文档

相关问题

相关 PR


Scaffold.resizeToAvoidBottomPadding

#

受修复工具支持:是

ScaffoldresizeToAvoidBottomPadding 参数在 v1.1.9 中已弃用。应改用 resizeToAvoidBottomInset 参数。

迁移指南

迁移前的代码

dart
Scaffold(resizeToAvoidBottomPadding: true);

迁移后的代码

dart
Scaffold(resizeToAvoidBottomInset: true);

参考资料

API 文档

相关问题

相关 PR


ButtonTheme.bar

#

受修复工具支持:否

ButtonThemebar 构造函数在 v1.9.1 中已弃用。对于 ButtonBar,可以改用 ButtonBarTheme;如果用途不特定于 ButtonBar,则使用 ButtonTheme 的另一个构造函数。

按钮特定的主题设置也可通过 TextButtonThemeElevatedButtonThemeOutlinedButtonTheme 类实现,每个类都与相应的按钮类 TextButtonElevatedButtonOutlinedButton 对应。

迁移指南

迁移前的代码

dart
ButtonTheme.bar(
  minWidth: 10.0,
  alignedDropdown: true,
  height: 40.0,
);

迁移后的代码,使用 ButtonTheme

dart
ButtonTheme(
  minWidth: 10.0,
  alignedDropdown: true,
  height: 40.0,
);

迁移后的代码,使用 ButtonBarTheme

dart
ButtonBarTheme(
  data: ButtonBarThemeData(
    buttonMinWidth: 10.0,
    buttonAlignedDropdown: true,
    buttonHeight: 40.0,
  )
);

参考资料

API 文档

相关问题

相关 PR


InlineSpanTextSpanPlaceholderSpan

#

受修复工具支持:否

为了实现在段落中内嵌小部件(如图片),InlineSpanTextSpanPlaceholderSpan 中的以下方法已弃用。

迁移指南

迁移前的代码迁移后的代码
InlineSpan.textTextSpan.text
InlineSpan.childrenTextSpan.children
InlineSpan.visitTextSpanInlineSpan.visitChildren
InlineSpan.recognizerTextSpan.recognizer
InlineSpan.describeSemanticsInlineSpan.computeSemanticsInformation
PlaceholderSpan.visitTextSpanPlaceHolderSpan.visitChildren
TextSpan.visitTextSpanTextSpan.visitChildren

参考资料

API 文档

相关问题

相关 PR


RenderView.scheduleInitialFrame

#

受修复工具支持:否

为防止启动画面过早消失导致黑屏,RenderView.scheduleInitialFrame 方法已弃用并移除。当调用 WidgetsFlutterBinding.ensureInitialized 时,会发生这种情况。请改用 RenderView.prepareInitialFrame 替换对此方法的调用,然后调用 RenderView.owner.requestVisualUpdate

迁移指南

迁移前的代码

dart
scheduleInitialFrame();

迁移后的代码

dart
prepareInitialFrame();
owner.requestVisualUpdate();

参考资料

API 文档

相关问题

相关 PR


Layer.findAll

#

受修复工具支持:否

为了统一 findfindAll 的实现,Layer.findAll 方法随着 Layer.findAnnotations 的引入而被弃用。要迁移受影响的代码,请改用 findAllAnnotations。此方法返回一个 AnnotationResult,其中包含 findAll 之前的返回值,存储在 AnnotationResult.annotations 中。

迁移指南

迁移前的代码

dart
findAll(offset);

迁移后的代码

dart
findAllAnnotations(offset).annotations;

参考资料

API 文档

相关问题

相关 PR


BinaryMessages

#

受修复工具支持:否

BinaryMessages 类、其关联的静态方法和 defaultBinaryMessenger getter 已弃用并移除。defaultBinaryMessenger 实例已移至 ServicesBinding。通过为测试创建 ServicesBinding 子类,可以在测试环境下注册不同的默认 BinaryMessenger。这样做可以跟踪待处理的平台消息数量,以实现同步目的。

迁移指南

迁移前的代码迁移后的代码
defaultBinaryMessengerServicesBinding.instance.defaultBinaryMessenger
BinaryMessagesBinaryMessenger
BinaryMessages.handlePlatformMessageServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage
BinaryMessages.sendServicesBinding.instance.defaultBinaryMessenger.send
BinaryMessages.setMessageHandlerServicesBinding.instance.defaultBinaryMessenger.setMessageHandler
BinaryMessages.setMockMessageHandlerServicesBinding.instance.defaultBinaryMessenger.setMockMessageHandler

参考资料

API 文档

相关问题

相关 PR


BuildContext 的泛型方法

#

受修复工具支持:是

BuildContext 中的一些方法使用 Type 来搜索祖先。这些方法大多在调用处隐含了类型转换,因为它们的返回类型是父类型。此外,即使类型实际上受到约束,所提供的类型在分析时也未经过检查。将这些方法泛型化可以提高类型安全性并减少代码量。

这些方法更改影响 BuildContextElementStatefulElement 类。TypeMatcher 类也已移除。

迁移指南

迁移前的代码

dart
ComplexLayoutState state = context.ancestorStateOfType(const TypeMatcher<ComplexLayoutState>()) as ComplexLayoutState;

迁移后的代码

dart
ComplexLayoutState state = context.ancestorStateOfType<ComplexLayoutState>();

BuildContext

迁移前的代码迁移后的代码
inheritFromElementdependOnInheritedElement
inheritFromWidgetOfExactTypedependOnInheritedWidgetOfExactType
ancestorInheritedElementForWidgetOfExactTypegetElementForInheritedWidgetOfExactType
ancestorWidgetOfExactTypefindAncestorWidgetOfExactType
ancestorStateOfTypefindAncestorStateOfType
rootAncestorStateOfTypefindRootAncestorStateOfType
ancestorRenderObjectOfTypefindAncestorRenderObjectOfType

Element

迁移前的代码迁移后的代码
inheritFromElementdependOnInheritedElement
inheritFromWidgetOfExactTypedependOnInheritedWidgetOfExactType
ancestorInheritedElementForWidgetOfExactTypegetElementForInheritedWidgetOfExactType
ancestorWidgetOfExactTypefindAncestorWidgetOfExactType
ancestorStateOfTypefindAncestorStateOfType
rootAncestorStateOfTypefindRootAncestorStateOfType
ancestorRenderObjectOfTypefindAncestorRenderObjectOfType

StatefulElement

迁移前的代码迁移后的代码
inheritFromElementdependOnInheritedElement

参考资料

API 文档

相关 PR


WidgetsBinding.deferFirstFrameReportWidgetsBinding.allowFirstFrameReport

#

受修复工具支持:是

为了提供延迟渲染第一帧的选项,WidgetsBindingdeferFirstFrameReportallowFirstFrameReport 方法已弃用并移除。这对于需要异步获取初始化信息的小部件很有用,当它们等待这些信息时,不应渲染任何帧,因为这会过早地移除启动画面。应分别改用 deferFirstFrameallowFirstFrame 方法。

迁移指南

迁移前的代码

dart
final WidgetsBinding binding = WidgetsBinding.instance;
binding.deferFirstFrameReport();
binding.allowFirstFrameReport();

迁移后的代码

dart
final WidgetsBinding binding = WidgetsBinding.instance;
binding.deferFirstFrame();
binding.allowFirstFrame();

参考资料

API 文档

相关 PR


WaitUntilNoTransientCallbacksWaitUntilNoPendingFrameWaitUntilFirstFrameRasterized

#

受修复工具支持:否

为了提供更具组合性的 waitForCondition API(可用于组合客户端希望等待的条件),flutter_driver 包中的 WaitUntilNoTransientCallbacksWaitUntilNoPendingFrameWaitUntilFirstFrameRasterized 方法已弃用并移除。

迁移指南

迁移前的代码迁移后的代码
WaitUntilNoTransientCallbacksWaitForCondition(NoTransientCallbacks())
WaitUntilNoPendingFrameWaitForCondition(NoPendingFrame())
WaitUntilFirstFrameRasterizedWaitForCondition(FirstFrameRasterized))

参考资料

API 文档

相关问题

相关 PR


时间线

#

稳定版本:2.0.0