Skip to main content

v1.22 之後移除的已棄用 API

在達到生命週期終止後, 下列已棄用的 API 已從 Flutter 中移除。

摘要

#

根據 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 標記為已淘汰(deprecated)。 此欄位成員在標記為已淘汰時即已無效。 此參數沒有替代方案,應將相關引用移除。

遷移指南

遷移前的程式碼:

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

遷移後的程式碼:

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

參考資料

API 文件:

相關議題:

相關 PR:


Pointer events constructed fromHoverEvent

#

支援修正工具:是

fromHoverEvent 的建構函式(constructors)在 PointerEnterEventPointerExitEvent 已於 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:

  • 已在 #28602 標記為不建議使用
  • 已在 #72395 移除

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 被標記為已淘汰(deprecated)。 針對 ButtonBar,可以改用 ButtonBarTheme, 若用途不限於 ButtonBar,也可使用 ButtonTheme 的其他建構函式。

針對按鈕的主題化(theming),也可使用 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 中被標記為已淘汰, 以支援將元件(Widgets)內嵌於段落中,例如圖片。

遷移指南

遷移前的程式碼遷移後的程式碼
InlineSpan.textTextSpan.text
InlineSpan.childrenTextSpan.children
InlineSpan.visitTextSpanInlineSpan.visitChildren
InlineSpan.recognizerTextSpan.recognizer
InlineSpan.describeSemantics InlineSpan.computeSemanticsInformation
PlaceholderSpan.visitTextSpan PlaceHolderSpan.visitChildren
TextSpan.visitTextSpanTextSpan.visitChildren

參考資料

API 文件:

相關議題:

相關 PR:


RenderView.scheduleInitialFrame

#

支援修正工具:否

RenderView.scheduleInitialFrame 方法已被標記為淘汰並移除, 以避免啟動畫面(splash screen)過早被移除, 導致出現黑畫面。 當呼叫 WidgetsFlutterBinding.ensureInitialized 時會發生此問題。 請改為使用 RenderView.prepareInitialFrame, 並接著呼叫 RenderView.owner.requestVisualUpdate 來取代此方法。

遷移指南

遷移前的程式碼:

dart
scheduleInitialFrame();

遷移後的程式碼:

dart
prepareInitialFrame();
owner.requestVisualUpdate();

參考資料

API 文件:

相關議題:

相關 PR:


Layer.findAll

#

支援修正工具:否

Layer.findAll 方法在引入 Layer.findAnnotations 時被標記為過時, 目的是統一 findfindAll 的實作方式。 若要遷移受影響的程式碼,請改為呼叫 findAllAnnotations。 此方法會回傳一個 AnnotationResult,其中包含原本 findAllAnnotationResult.annotations 中的回傳值。

遷移指南

遷移前的程式碼:

dart
findAll(offset);

遷移後的程式碼:

dart
findAllAnnotations(offset).annotations;

參考資料

API 文件:

相關議題:

相關 PR:


BinaryMessages

#

支援修正工具:否

BinaryMessages 類別、其相關的靜態方法以及 defaultBinaryMessenger getter 已被棄用並移除。defaultBinaryMessenger 實例已移至 ServicesBinding。這使得在測試環境下,可以註冊不同的預設 BinaryMessenger,只需為測試建立 ServicesBinding 子類別即可。這樣做可以讓你追蹤待處理平台訊息的數量,以利同步用途。

遷移指南

遷移前程式碼:遷移後程式碼:
defaultBinaryMessenger ServicesBinding.instance.defaultBinaryMessenger
BinaryMessagesBinaryMessenger
BinaryMessages.handlePlatformMessage ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage
BinaryMessages.send ServicesBinding.instance.defaultBinaryMessenger.send
BinaryMessages.setMessageHandler ServicesBinding.instance.defaultBinaryMessenger.setMessageHandler
BinaryMessages.setMockMessageHandler ServicesBinding.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
inheritFromWidgetOfExactType dependOnInheritedWidgetOfExactType
ancestorInheritedElementForWidgetOfExactType getElementForInheritedWidgetOfExactType
ancestorWidgetOfExactTypefindAncestorWidgetOfExactType
ancestorStateOfTypefindAncestorStateOfType
rootAncestorStateOfTypefindRootAncestorStateOfType
ancestorRenderObjectOfTypefindAncestorRenderObjectOfType

Element

遷移前的程式碼:遷移後的程式碼:
inheritFromElementdependOnInheritedElement
inheritFromWidgetOfExactType dependOnInheritedWidgetOfExactType
ancestorInheritedElementForWidgetOfExactType getElementForInheritedWidgetOfExactType
ancestorWidgetOfExactTypefindAncestorWidgetOfExactType
ancestorStateOfTypefindAncestorStateOfType
rootAncestorStateOfTypefindRootAncestorStateOfType
ancestorRenderObjectOfTypefindAncestorRenderObjectOfType

StatefulElement

遷移前的程式碼:遷移後的程式碼:
inheritFromElementdependOnInheritedElement

參考資料

API 文件:

相關 PR:


WidgetsBinding.deferFirstFrameReport & WidgetsBinding.allowFirstFrameReport

#

支援修正工具:是

WidgetsBindingdeferFirstFrameReportallowFirstFrameReport 方法 已被標記為淘汰並移除,以便提供延遲首次繪製畫面的選項。 這對於需要非同步取得初始化資訊的元件(Widgets)非常有用, 在等待這些資訊的期間,不應繪製任何畫面,否則會導致啟動畫面過早消失。 請分別改用 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,讓用戶端能夠自行組合想要等待的條件。

遷移指南

遷移前程式碼:遷移後程式碼:
WaitUntilNoTransientCallbacks WaitForCondition(NoTransientCallbacks())
WaitUntilNoPendingFrame WaitForCondition(NoPendingFrame())
WaitUntilFirstFrameRasterized WaitForCondition(FirstFrameRasterized))

參考資料

API 文件:

相關議題:

相關 PR:


時程表

#

穩定版發佈:2.0.0