fix(app): web 上不碰 dart:io——沒帶 API_BASE 開瀏覽器不再噴看不懂的錯 - #103
Open
thothawei wants to merge 1 commit into
Open
Conversation
症狀:`flutter run -d chrome` 沒帶 --dart-define=API_BASE 時,畫面直接是 「Unsupported operation: Platform._operatingSystem」。訊息完全看不出跟 API 位址或平台選擇有關,排查成本很高。 機制:AppConfig.apiBase 在沒有 API_BASE 覆寫時會讀 dart:io 的 Platform.isIOS。 Flutter web 的 dart:io 是會拋例外的 stub,於是啟動即死。文件記載的乘客端 指令有帶 --dart-define,所以一直沒被發現;一旦少帶就必炸。 修正 - AppConfig.apiBase 先判斷 kIsWeb 回傳 http://localhost:8080。kIsWeb 是 編譯期常數,dart:io 分支在 web build 會被整段消除 - 司機端(lib/main.dart 與 lib/main_driver.dart)在 kIsWeb 時改顯示明確的 「不支援瀏覽器」畫面。司機端依賴背景定位與系統權限對話框,在瀏覽器不可能 運作,另外還有四處 dart:io Platform 呼叫;與其在半路炸,不如講清楚 測試 新增 test/core/config/app_config_web_test.dart,標記 @teston('browser')。 必須這樣跑才有意義: flutter test --platform chrome test/core/config/app_config_web_test.dart 理由:一般 flutter test 跑在 VM 上 kIsWeb 恆為 false,把防線整條拔掉也不會 紅。已做反向驗證——還原成修正前的 app_config,三條測試紅兩條。 注意:現行 CI 只跑 VM 測試,不會跑到這支。要納入 CI 需要在 flutter-ci.yml 加一個帶 Chrome 的 job,但推 .github/workflows 需要 token 的 workflow scope, 本次未處理。 驗證 - flutter analyze --fatal-infos 零 issue - flutter test 448 項全過 - flutter build web -t lib/main_customer.dart 後實際載入:登入畫面正常, console 無錯誤,且刻意未帶 API_BASE(正是修正前會崩潰的情境) - flutter build web -t lib/main.dart 後實際載入:顯示「司機端不支援瀏覽器」 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
症狀
flutter run -d chrome沒帶--dart-define=API_BASE時,畫面直接是:訊息完全看不出跟 API 位址或平台選擇有關。
機制
AppConfig.apiBase在沒有API_BASE覆寫時會讀dart:io的Platform.isIOS。Flutter web 的
dart:io是會拋例外的 stub,於是啟動即死。docs/API_KEYS_SETUP.md記載的乘客端指令有帶--dart-define=API_BASE,會在碰到
Platform之前就 return,所以這個洞一直沒被觸發——一旦少帶參數就必炸。修正
AppConfig.apiBase先判斷kIsWeb回傳http://localhost:8080。kIsWeb是編譯期常數,
dart:io分支在 web build 會被整段消除。lib/main.dart、lib/main_driver.dart)在kIsWeb時顯示明確的「不支援瀏覽器」畫面。司機端依賴背景定位與系統權限對話框,在瀏覽器不可能運作,
另外還有四處
dart:io Platform呼叫;與其在半路炸,不如講清楚。lib/main.dart是flutter run -d chrome的預設進入點,正是最容易踩到的那條。測試
新增
test/core/config/app_config_web_test.dart,標記@TestOn('browser'):flutter test --platform chrome test/core/config/app_config_web_test.dart一般
flutter test跑在 VM 上,kIsWeb恆為 false,把防線整條拔掉也不會紅,那種測試等於沒有。已做反向驗證:還原成修正前的
app_config,三條測試紅兩條。flutter-ci.yml加一個帶 Chrome 的 job;推
.github/workflows需要 token 的workflowscope,本次未處理。驗證
flutter analyze --fatal-infos零 issueflutter test448 項全過flutter build web -t lib/main_customer.dart後實際載入:登入畫面正常、console 無錯誤,且刻意未帶
API_BASE——正是修正前會崩潰的情境flutter build web -t lib/main.dart後實際載入:顯示「司機端不支援瀏覽器」範圍說明
司機端那段不在原始需求範圍內,是我在查證時發現「只修
app_config的話,預設的
flutter run -d chrome仍會在driver_controller.dart:37換個地方炸」,所以一併處理。不想要的話可以只留第一個 commit hunk。
🤖 Generated with Claude Code