發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):4426
在 Flutter 里有幾種調(diào)試方式。
在 Flutter 進行斷點調(diào)試非常簡單,只需要在 Vscode 上打上一個斷點,按 F5 就會停在斷點處。通過左邊的調(diào)試欄,觀察斷點處的變量以及棧堆情況。
除了打斷點,也可以使用 debugger API 的方式。
import 'dart:developer';void someFunction(double offset) { debugger(when: offset > 30.0, message: 'offset 大于 30 時,中斷'); // ... }
這段代碼表明,當 offset 這個變量的值大于 30.0 時中斷運行,并輸出 message 的內(nèi)容。debugger 代碼只會在開發(fā)階段運行。
rendering 即開啟布局線,當打開 rendering 時,會在界面上看到一些布局線,以便于修復(fù)布局效果。
import 'package:flutter/rendering.dart';void main() { debugPaintSizeEnabled = !true; runApp(new MyApp()); }
打開之后看到的效果。
日志調(diào)試,直接使用 print 輸出內(nèi)容即可,在 AS、Vscode 里的控制臺/調(diào)試控制臺都可以看到。
為了方便,定義一個 Debug 類。
class Debug { static log(String tag, String text) { print('[$tag] $text'); } static info(String tag, String text) { print('[$tag] $text'); } static success(String tag, String text) { print('[$tag] $text'); } static error(String tag, String text) { print('[$tag] $text'); } }
在使用命令:flutter run
時,會有一個 Observatory URL 地址提供(一般為:http://127.0.0.1:8108/),那就是運行狀態(tài)調(diào)試,打開之后可以看到應(yīng)用的 GC、VM 方面的信息。注意,如果使用 F5 啟動,則沒有這個功能。
開啟真機調(diào)試的步驟:
然后可通過執(zhí)行 flutter run 運行我們的 app。
在手機上開啟 USB 調(diào)試模式。