跳至主要內容

v1.22 後移除的已棄用 API

摘要

#

根據 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 中已棄用。ButtonBarTheme 可用於 ButtonBar,或者如果使用不特定於 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.findAnnotations 時,Layer.findAll 方法已棄用。若要遷移受影響的程式碼,請改為呼叫 findAllAnnotations。此方法會傳回 AnnotationResult,其中包含 AnnotationResult.annotationsfindAll 的先前傳回值。

遷移指南

遷移前的程式碼

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.deferFirstFrameReport & WidgetsBinding.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

#

修復工具支援:否

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

遷移指南

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

參考資料

API 文件

相關問題

相關的 PR


時間軸

#

穩定版本:2.0.0