Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Double page instantiation when navigating straight to deep path #336

@robertoestivill

Description

@robertoestivill

I'm having an issue when navigating to a deep url path, with parent paths/routes being instantiated.

Basically, if I navigate to /page/content and I have another route for /page, both are instantiated, though /page/content is only displayed.
In my particular case, there's tracking code that is being executed on both pages, even if /page/content is the only one being opened.

I've seen this behavior in the app I'm working on, but this is an app I've created to reproduce the issue in the simplest way.

import 'package:flutter/material.dart';
import 'package:routemaster/routemaster.dart';

void main() {
  Routemaster.setPathUrlStrategy();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    final routeMaster = RoutemasterDelegate(
      routesBuilder: (BuildContext context) {
        return RouteMap(
          routes: {
            '/page': (RouteData route) {
              print("Route /page1");
              return SimplePage(title: '/page', color: Colors.red);
            },
            '/page/content': (RouteData route) {
              print("Route /page1/content");
              return SimplePage(title: '/page/content', color: Colors.blue);
            },
          },
        );
      },
    );

    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      routerDelegate: routeMaster,
      routeInformationParser: const RoutemasterParser(),
    );
  }
}

class SimplePage extends MaterialPage<void> {
  SimplePage({required String title, required Color color})
    : super(child: Container(color: color)) {
    print('Page $title');
  }
}

Once you have it running, pointing the browser to /page/content will instantiate both routes and pages, effectively printing

Performing hot restart...
Waiting for connection from debug service on Chrome...
Restarted application in 69ms.
Route /page1/content
Page /page/content
Route /page1
Page /page

Is this behavior documented somewhere? why is the expectation that the parent route/page is running?
This can be pretty bad, specially if the parent page is running some sort of resource intensive process.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions