flutter 3.13 又更新了,與Incorrect use of ParentDataWidget問題

 

等它更新....要花不少時間,可以去喝杯茶...

但是不要高興太早,
還是一樣遇到問題:
其實flutter並不是表面上看起來那麼簡單,還是跟android or ios 一樣會出現許多隱藏式錯誤.




======== Exception caught by widgets library =======================================================

The following assertion was thrown while applying parent data.:

Incorrect use of ParentDataWidget.


The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.


Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.

The offending Expanded is currently placed inside a Padding widget.


The ownership chain for the RenderObject that received the incompatible parent data was:

  Column ← Expanded ← Padding ← DecoratedBox ← ConstrainedBox ← Container ← Expanded ← Column ← Semantics ← DefaultTextStyle ← ⋯



solution:

你遇到的錯誤訊息 "Incorrect use of ParentDataWidget" 表示你正在試圖以不允許的方式使用 Expanded widget。Expanded widget 應該被用在支援它的父級 widget 內,例如 ColumnRowFlex,以指定它的子 widget 應如何填充可用空間。將 Expanded widget 放在 Padding widget 內可能會導致此錯誤。

要解決這個問題,請確保你在適當的父級 widget 內使用 Expanded widget。以下是正確使用的方法:

  1. Expanded widget 包裹在支援的父級 widget 內:如果你打算使用 Expanded 來控制一個 widget 如何填充可用空間,應將它直接放在 ColumnRowFlex widget 內。

    dart
    Column( children: [ Expanded( flex: 1, child: YourWidget(), ), ], )
  2. 重新評估你的佈局:如果你嘗試在不同的上下文中使用 Expanded,請重新評估你的佈局結構。你可能需要調整 widget 階層結構以達到你期望的設計。

以下是在 Column 內正確使用 Expanded 的示例:

dart
Column( children: [ Expanded( flex: 1, child: Container( color: Colors.red, ), ), Container( color: Colors.blue, ), ], )

在這個示例中,使用 Expanded 包裹的第一個 Container 將佔據所有可用的垂直空間,將第二個 Container 推到 Column 的底部。

確保 Expanded widget 被用在能夠支援它的父級 widget 內,例如 ColumnRowFlex



來看看code:

原來錯誤的地方是expanded ,紅字的部分,移除就好了.

所以要檢查一下所有程式碼,是否有多餘的expanded.

child: Expanded(
child: Column(
children: [
Icon(
isLightOnList[index]
?
Icons.light_mode_outlined
: Icons.lightbulb_outline,
size: 48,
color:
isLightOnList[index] ? Colors.white : Colors.grey,
),
const SizedBox(height: 16),
Text(
'Light ${index + 1}',
style: TextStyle(
fontSize: 14,
color: isLightOnList[index]
?
Colors.white
: Colors.grey,
fontWeight: FontWeight.bold,
),
),

留言

這個網誌中的熱門文章

最爛的銀行服務-玉山銀行

Mark App Design Apps - Terms and Privacy Policy (服務條款,隱私權政策)

SMR疊瓦式hdd致命缺陷被解決????!!!

google play 正式發布前測試報告...非常好用.

更改google drive預設存放目錄位置Change Google Drive Default Folder Location in Windows

ios app 上架時app icon要注意事項

舊有app在Android 12 閃退問題& app Splash screens

app bundle and bundletool. 關於aab安裝問題

關於google play console app應用程式簽署

Google Play badge徽章產生器