Skip to content

[已解決] Error: This requires the null safety language feature, which is experimental. You can enable the experiment using the ‘–enable-experiment=non-nullable’ command line option.

今天當我在使用 Flutter 進行手機 APP 開發時,當我使用自定義的 Icon 圖示來製作按鈕後,我在執行程式碼時得到了以下的錯誤訊息:

Error: This requires the null safety language feature, which is experimental. You can enable the experiment using the '--enable-experiment=non-nullable' command line option.


簡單來說,我所匯入的 icon 圖示的文件,使用了當前 SDK 版本還在實驗性質的語法:

並且,IDE 自動偵測語法後推薦我升級 SDK 版本至少到 2.10.0


解決方法

解決這個問題需要兩個步驟:

  • 確認 Flutter 版本(若版本過低,即便調整下方 SDK 版本也無法成功讓環境運作
  • 在 pubspec.yaml 文件中調整 environment 參數底下的 sdk 版本。

(可以先調整 SDK 版本,若無法順利讓環境運作,再來升級 Flutter 版本)


(Optional) 升級 Flutter 版本

可以使用以下指令升級 Flutter 版本:

flutter upgrade


調整 SDK 版本

在 pubspec.yaml 文件中,添加設定:

environment:
  sdk: ">=2.12.0 <3.0.0"

接著,使用以下指令:

flutter pub get


完成之後可能需要重新啟動 IDE 才能看到語法解析正常顯示。

至此,應該可以正常執行程式而不會報錯了。


References


Read More

Tags:

Leave a Reply