Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route Arguments are always null inside AutoRouterObserver when using navigateNamed #1756

Closed
FlorentineDraegerProtofy opened this issue Oct 11, 2023 · 1 comment

Comments

@FlorentineDraegerProtofy
Copy link

FlorentineDraegerProtofy commented Oct 11, 2023

When using navigateNamed to navigate a page with route parameters, the route arguments cannot be accessed inside didPush method of the AutoRouterObserver. While the arguments are correctly being passed to the page, they are not accessible inside the observer.

Main:

import 'package:auto_router_test/app_router.dart';
import 'package:auto_router_test/routing_observer.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  // make sure you don't initiate your router
  // inside of the build function.
  final _appRouter = AppRouter();

  App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
        routerConfig: _appRouter.config(
      navigatorObservers: () => [RoutingObserver()],
    ));
  }
}

Observer:

import 'package:auto_route/auto_route.dart';

import 'package:flutter/material.dart';

class RoutingObserver extends AutoRouterObserver {
  @override
  void didPush(Route<dynamic> targetRoute, Route<dynamic>? previousRoute) {
    print('did push: ${targetRoute.settings.arguments}');
  }
}
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

@RoutePage()
class MySecondPage extends StatefulWidget {
  const MySecondPage({super.key, @PathParam() required this.id});
  final String id;

  @override
  State<MySecondPage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MySecondPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'This is route argument id:',
            ),
            Text(
              widget.id,
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
    );
  }
}
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

@RoutePage()
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _goToSecondPage() {
    context.router.navigateNamed('/second/1234');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('hi'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'hello world',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _goToSecondPage,
        tooltip: 'go to second page',
        child: const Icon(Icons.arrow_right),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Output: flutter: did push: null

@Milad-Akarie
Copy link
Owner

@FlorentineDraegerProtofy this is how you would access path/query params targetRoute.data?.pathParams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants