Skip to content

fix(app): web 上不碰 dart:io——沒帶 API_BASE 開瀏覽器不再噴看不懂的錯 - #103

Open
thothawei wants to merge 1 commit into
mainfrom
claude/fix-web-platform-crash
Open

fix(app): web 上不碰 dart:io——沒帶 API_BASE 開瀏覽器不再噴看不懂的錯#103
thothawei wants to merge 1 commit into
mainfrom
claude/fix-web-platform-crash

Conversation

@thothawei

Copy link
Copy Markdown
Owner

症狀

flutter run -d chrome 沒帶 --dart-define=API_BASE 時,畫面直接是:

Unsupported operation: Platform._operatingSystem

訊息完全看不出跟 API 位址或平台選擇有關。

機制

AppConfig.apiBase 在沒有 API_BASE 覆寫時會讀 dart:ioPlatform.isIOS
Flutter web 的 dart:io 是會拋例外的 stub,於是啟動即死。

docs/API_KEYS_SETUP.md 記載的乘客端指令有帶 --dart-define=API_BASE,會在
碰到 Platform 之前就 return,所以這個洞一直沒被觸發——一旦少帶參數就必炸。

修正

  • AppConfig.apiBase 先判斷 kIsWeb 回傳 http://localhost:8080kIsWeb
    編譯期常數,dart:io 分支在 web build 會被整段消除。
  • 司機端(lib/main.dartlib/main_driver.dart)在 kIsWeb 時顯示明確的
    「不支援瀏覽器」畫面。司機端依賴背景定位與系統權限對話框,在瀏覽器不可能運作,
    另外還有四處 dart:io Platform 呼叫;與其在半路炸,不如講清楚。
    lib/main.dartflutter 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,三條測試紅兩條。

⚠️ 現行 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 後實際載入:顯示「司機端不支援瀏覽器」

範圍說明

司機端那段不在原始需求範圍內,是我在查證時發現「只修 app_config 的話,
預設的 flutter run -d chrome 仍會在 driver_controller.dart:37 換個地方炸」,
所以一併處理。不想要的話可以只留第一個 commit hunk。

🤖 Generated with Claude Code

症狀:`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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant