デバッグとロギング
Flutterデバッグとロギング完全ガイド
Section titled “Flutterデバッグとロギング完全ガイド”Flutterアプリのデバッグとロギングの実践的な方法を詳しく解説します。
1. 基本的なデバッグ方法
Section titled “1. 基本的なデバッグ方法”print文の使用
Section titled “print文の使用”void someFunction() { print('Debug message'); print('Variable value: $variable');}debugPrintの使用
Section titled “debugPrintの使用”import 'package:flutter/foundation.dart';
void someFunction() { debugPrint('Debug message'); // 大量の出力でも問題なし}2. ロギングライブラリの使用
Section titled “2. ロギングライブラリの使用”loggerパッケージ
Section titled “loggerパッケージ”dependencies: logger: ^2.0.0import 'package:logger/logger.dart';
final logger = Logger( printer: PrettyPrinter(),);
void someFunction() { logger.d('Debug message'); logger.i('Info message'); logger.w('Warning message'); logger.e('Error message', error: e, stackTrace: stackTrace);}3. Flutter DevToolsの活用
Section titled “3. Flutter DevToolsの活用”パフォーマンスプロファイリング
Section titled “パフォーマンスプロファイリング”flutter run --profile# DevToolsを開くflutter pub global run devtoolsメモリプロファイリング
Section titled “メモリプロファイリング”- Memoryタブでメモリ使用量を監視
- Heap Snapshotでメモリのスナップショットを取得
4. 実務でのデバッグ手法
Section titled “4. 実務でのデバッグ手法”ブレークポイントの設定
Section titled “ブレークポイントの設定”VS CodeやAndroid Studioでブレークポイントを設定し、デバッグ実行します。
ログレベルの管理
Section titled “ログレベルの管理”class AppLogger { static final Logger _logger = Logger( printer: PrettyPrinter(), level: kDebugMode ? Level.debug : Level.warning, );
static void d(String message) => _logger.d(message); static void i(String message) => _logger.i(message); static void w(String message) => _logger.w(message); static void e(String message, {Object? error, StackTrace? stackTrace}) { _logger.e(message, error: error, stackTrace: stackTrace); }}これで、Flutterでのデバッグとロギングの実装方法を理解できるようになりました。