前言,Flutter的熱重載(hot reload)功能可以幫助您在無需重新啟動應用的情況下快速、輕鬆地進行測試、構建用戶界面、添加功能以及修複錯誤。 通過將更新後的源代碼文件註入正在運行的Dart虛擬機(VM)中來實現熱重載。 在虛擬機使用新的的欄位和函數更新類後,Flutter框架會自動重新構建 ...
前言,Flutter的熱重載(hot reload)功能可以幫助您在無需重新啟動應用的情況下快速、輕鬆地進行測試、構建用戶界面、添加功能以及修複錯誤。
通過將更新後的源代碼文件註入正在運行的Dart虛擬機(VM)中來實現熱重載。
在虛擬機使用新的的欄位和函數更新類後,Flutter框架會自動重新構建widget樹,以便您快速查看更改的效果。
要熱重載一個Flutter應用程式:
-
從受支持的IntelliJ IDE 、Android Studio 或終端視窗運行應用程式。物理機或虛擬器都可以運行。
-
修改項目中的一個Dart文件。大多數類型的代碼更改可以重新載入; 有關需要完全重新啟動的更改列表,請參閱限制。
-
如果您使用的是IntelliJ IDE 、Android Studio ,請選擇Save All (
cmd-s
/ctrl-s
)),或者單擊工具欄上的Hot Reload按鈕。
一、現象:
Flutter 不能熱載入 熱重載按鈕灰色,無法點擊。
二、分析原因:
終端
flutter doctor 顯示 NO_PROXY is not set
沒有設置無代理的埠
終端:flutter doctor
➜ ~ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8, on Mac OS X 10.14.3 18D109, locale
zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.41.1)
[!] Proxy Configuration
! NO_PROXY is not set
[✓] Connected device (1 available)
! Doctor found issues in 1 category.
重點在於:NO_PROXY is not set
三、解決方案:
設置 NO_PROXY即可
1.啟動終端
2.進入當前用戶的home目錄
cd ~ 或者 cd /users/YourMacUserName
3.輸入touch .bash_profile
查看和編輯.bash_profile文件
1.終端輸入open -e .bash_profile
2.編輯文件,新增
export no_proxy="localhost,127.0.0.1"
3.關閉保存
最後更新配置的環境變數
輸入:source .bash_profile
不懂這塊的話,看這裡設置Mac 終端走代理
操作如下:
➜ ~ cd ~
➜ ~ touch .bash_profile
➜ ~ open -e .bash_profile
➜ ~ source .bash_profile
➜ ~ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8, on Mac OS X 10.14.3 18D109, locale
zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.41.1)
[✓] Proxy Configuration
[✓] Connected device (1 available)
• No issues found!
➜ ~
四、驗證
運行時,熱重載按鈕變為橙色,可以點擊。
快捷鍵保存編輯時,自動重載。