8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FlutterでGoRouterのログを出力する(メモ)

Posted at

Flutterをはじめ、アプリ開発をしている時、度々画面遷移の前後でエラーが発生することはないでしょうか?

また、それが画面遷移の前なのかあとなのか、
あと、本日紹介するGoRouterの場合、どこで定義したパスで発生したのかなどがわかると原因の特定がしやすくなると思います。

今回はその小ネタを紹介します。

対象パッケージ

コード

実装はとても簡単です。
GoRouterを定義する際に以下のようにdebugLogDiagnosticsを定義するだけです

final router = GoRouter(
  debugLogDiagnostics: true, // trueに設定
  initialLocation: SplashPage.routePath,
  /// ルート定義
  routes: [
    // ...
  ],
);

挙動

ビルド時

このフラグを指定することで、ビルド時は以下のように定義してあるルートを出力してくれます。
nameも定義してある場合、nameとpathを紐づけて出力します。

[GoRouter] Full paths for routes:
             => /
             => /home
             =>   /home/sample_detail
           known full paths for route names:
             splashpage => /
             homepage => /home
             sampledetailpage => /home/sample_detail
[GoRouter] setting initial location /
[GoRouter] Using MaterialApp configuration

遷移時

画面遷移時もnameとpathを出力してくれます。

goNamedで遷移した場合

[GoRouter] getting location for name: "homePage"
[GoRouter] going to /home

pushNamedで遷移した場合

[GoRouter] getting location for name: "homePage"
[GoRouter] pushing /home

ただし、url_launcherパッケージのlaunchUrl()メソッドの場合やNavigatorで遷移した場合はこのログは出力されません。あくまでGoRouterのメソッドを用いた場合となるのでご注意ください。

8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?