預設停用 iOS 與 Android 上的不安全 HTTP 連線
除非網域已被政策明確允許,否則存取 HTTP 協定的 URL 會拋出例外。
摘要
#如果您的程式碼嘗試在 iOS 或 Android 上對主機建立 HTTP 連線,現在會拋出 StateException,並顯示以下訊息:
Insecure HTTP is not allowed by platform: <host>
請改用 HTTPS。
背景說明
#自 Android API 28 及 iOS 9 起,這些平台預設會停用不安全的 HTTP 連線。
此變更使 Flutter 也在行動平台上停用不安全的連線。其他平台(桌面、網頁等)則不受影響。
你可以依照各平台的指引,定義特定網域的網路政策來覆寫此行為。詳情請參考下方的遷移指南。
遷移指南
#在 iOS 上,你可以在應用程式的 Info.plist 中加入 NSExceptionDomains。
在 Android 上,你可以新增一個 network security config XML 檔案。
為 Debug 版本允許明文連線
#
如果你希望在 Android 的 debug 版本中允許 HTTP 連線,可以將以下程式碼片段加入到你的 $project_path\android\app\src\debug\AndroidManifest.xml:
<application android:networkSecurityConfig="@xml/network_security_config">
...
</application>
接著,將網路設定加入你的 $project_path/android/app/src/debug/res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
也可以針對個別網域設定政策。詳情請參閱 Android 說明文件。
對於 iOS,你可以依照這些指引來建立 Info-debug.plist,並將以下內容放入其中:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我們不建議你在正式發行(release)版本中這麼做。
其他資訊
#- 建置時(build time)設定是唯一能更改網路政策的方式。無法在執行時(runtime)修改。
時程
#
納入版本:1.23
穩定版發行:2.0.0
於版本 2.2.0 回復(提案中)
參考資料
#API 文件:此變更沒有 API,因為網路政策的修改是透過上述的平台專屬設定完成。
相關 PR:
Unless stated otherwise, the documentation on this site reflects Flutter 3.44.0. Page last updated on 2026-06-14. View source or report an issue.