はじめに
画面遷移でgo_routerを使っていて、initialLocationを設定しても効いていないことがあった。 原因はMaterialApp.routerの引数にrouteInformationProviderをセットし忘れていたからだった。
原因を調べていたときにFlutter公式では下記のようにrouterConfigに GoRouterオブジェクトをセットしていて、routerDelegateやrouteInformationParserなどを使っていない。
MaterialApp.router( routerConfig: GoRouter( // … ) );
画面遷移でgo_routerを使い始めたときにはなかった方法 or 勉強に利用したサイトがそういうやりかたをしていたのだろう。 routerDelegateやrouteInformationParserなどをセットしなくてもいいやり方があるのかと思い、 routerConfigについて少し調べたのでそのメモを残す。
RouterConfigについて
RouterConfigの定義は下記のとおり。
/// A convenient bundle to configure a [Router] widget. /// /// To configure a [Router] widget, one needs to provide several delegates, /// [RouteInformationProvider], [RouteInformationParser], [RouterDelegate], /// and [BackButtonDispatcher]. This abstract class provides way to bundle these /// delegates into a single object to configure a [Router]. /// /// The [routerDelegate] must not be null. The [backButtonDispatcher], /// [routeInformationProvider], and [routeInformationProvider] are optional. /// /// The [routeInformationProvider] and [routeInformationParser] must /// both be provided or not provided. class RouterConfig<T> { /// Creates a [RouterConfig]. /// /// The [routerDelegate] must not be null. The [backButtonDispatcher], /// [routeInformationProvider], and [routeInformationParser] are optional. /// /// The [routeInformationProvider] and [routeInformationParser] must /// both be provided or not provided. const RouterConfig({ this.routeInformationProvider, this.routeInformationParser, required this.routerDelegate, this.backButtonDispatcher, }) : assert((routeInformationProvider == null) == (routeInformationParser == null)); /// The [RouteInformationProvider] that is used to configure the [Router]. final RouteInformationProvider? routeInformationProvider; /// The [RouteInformationParser] that is used to configure the [Router]. final RouteInformationParser<T>? routeInformationParser; /// The [RouterDelegate] that is used to configure the [Router]. final RouterDelegate<T> routerDelegate; /// The [BackButtonDispatcher] that is used to configure the [Router]. final BackButtonDispatcher? backButtonDispatcher; }
A convenient bundle to configure a [Router] widget.
コメントの一文が示している通り、まとめて設定してくれる。 まとめて設定してくれる対象は下記のとおりでした。
- routerDelegate
- backButtonDispatcher
- routeInformationProvider
- routeInformationProvider