概述

#

根據 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 構建的 Pointer 事件

#

由修復工具支援:是

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


InlineSpan, TextSpan, PlaceholderSpan

#

由修復工具支援:否

為了能夠將 widget 內聯嵌入段落中(例如影像),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

#

由修復工具支援:否

Layer.findAll 方法隨著 Layer.findAnnotations 的引入而被棄用,目的是統一 findfindAll 的實現。要遷移受影響的程式碼,請改用 findAllAnnotations。此方法返回一個 AnnotationResult,其中包含 findAll 的前返回值,位於 AnnotationResult.annotations 中。

遷移指南

遷移前的程式碼

dart
findAll(offset);

遷移後的程式碼

dart
findAllAnnotations(offset).annotations;

參考資料

API 文件

相關問題

相關 PR


BinaryMessages

#

由修復工具支援:否

BinaryMessages 類、其相關的靜態方法和 defaultBinaryMessenger getter 已被棄用並移除。defaultBinaryMessenger 例項已移至 ServicesBinding。這使得在測試環境中註冊不同的預設 BinaryMessenger 成為可能,方法是建立一個用於測試的 ServicesBinding 子類。這樣做可以讓你跟蹤待處理的平臺訊息數量以進行同步。

遷移指南

遷移前的程式碼遷移後的程式碼
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 方法已被棄用並移除,目的是提供延遲渲染第一幀的選項。這對於需要非同步獲取初始化資訊的 widget 很有用,並且在等待該資訊時,不應渲染任何幀,因為這會過早關閉啟動畫面。應分別使用 deferFirstFrameallowFirstFrame 方法來代替。

遷移指南

遷移前的程式碼

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

遷移後的程式碼

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

參考資料

API 文件

相關 PR


WaitUntilNoTransientCallbacksWaitUntilNoPendingFrameWaitUntilFirstFrameRasterized

#

由修復工具支援:否

flutter_driver 包中的 WaitUntilNoTransientCallbacksWaitUntilNoPendingFrameWaitUntilFirstFrameRasterized 方法已被棄用並移除,目的是提供一個更具組合性的 waitForCondition API,可用於組合客戶端想要等待的條件。

遷移指南

遷移前的程式碼遷移後的程式碼
WaitUntilNoTransientCallbacksWaitForCondition(NoTransientCallbacks())
WaitUntilNoPendingFrameWaitForCondition(NoPendingFrame())
WaitUntilFirstFrameRasterizedWaitForCondition(FirstFrameRasterized))

參考資料

API 文件

相關問題

相關 PR


時間線

#

穩定版本:2.0.0