💭

go_router_builderで値を渡したい 詰まった!

2024/02/19に公開

GoRouterBuilderとは

https://pub.dev/documentation/go_router_builder/latest/

詰まったところ

required this.tag だと値を渡せない。

@MacBook % flutter pub run build_runner build --delete-conflicting-outputs
Deprecated. Use `dart run` instead.
[INFO] Generating build script completed, took 247ms
FlutterGen v5.4.0 Loading ... pubspec.yaml
[INFO] Reading cached asset graph completed, took 378ms
[INFO] Checking for updates since last build completed, took 1.5s
[SEVERE] go_router_builder on lib/provider/router/app_router.dart:

The parameter type `TagModel` is not supported.
package:friendy/provider/router/app_router.dart:126:46
    ╷
126 │   const SearchDetailRouteData({required this.tag});
    │                                              ^^^
    ╵
[INFO] Running build completed, took 11.9s
[INFO] Caching finalized dependency graph completed, took 205ms
[SEVERE] Failed after 12.1s
class SearchDetailRouteData extends GoRouteData {
  const SearchDetailRouteData({required this.tag});
  final TagModel tag;

  
  Widget build(BuildContext context, GoRouterState state) {
    return SearchDetailPage(tag: tag);
  }
}

解決策

$extrasを使用する。

class SearchDetailRouteData extends GoRouteData {
  const SearchDetailRouteData({required this.$extras});
  final TagModel $extras;

  
  Widget build(BuildContext context, GoRouterState state) {
    return SearchDetailPage(tag: $extras);
  }
}

state.extras でも 大丈夫

 
  Widget build(BuildContext context, GoRouterState state) {
    return SearchDetailPage(tag: state.extras as Model);
  }

Discussion