Repository navigation
Description
Consider an initial route like "/screen2" - this initial route automatically leads to the creation of both "/" and "/screen2", with the 2nd screen placed on top of the first. Both widgets are instantiated, both widgets are initialized, both widgets are built, and if both screens implement RouteAware, then both screens have their didPush() method invoked.
However, screen 1, AKA "/", never has its didPushNext() invoked.
This issue was raised but previously closed:
#55949 (comment)
I don't think that issue should have been closed. I believe the RouteAware contract is not being honored. The API doc for didPushNext() reads:
/// Called when a new route has been pushed, and the current route is no
/// longer visible.
Even though route "/" and route "/screen2" are built back to back as quickly as possible, they are absolutely different route objects, with different route names, and "/screen2" does cause "/" to lose visibility. Therefore, "didPushNext()" should be invoked.
In my particular case, I'm dealing with a QR code plugin, which expects a RenderBox to report a size as soon as the QR widget is initialized. This is a poor assumption, but I don't have control over that plugin. I need to be able to avoid building that QR view widget when "/" is not visible. I currently don't see any non-hacky way to handle that because didPushNext() is not invoked as per its contract.
Here is a complete and minimal reproduction of the situation:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
final RouteObserver _routeObserver = RouteObserver();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bug - Route Aware, Initial Route',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
initialRoute: '/screen2',
routes: {
'/': (_) => Screen1(),
'/screen2': (_) => Screen2(),
},
navigatorObservers: [_routeObserver],
);
}
}
class Screen1 extends StatefulWidget {
@override
_Screen1State createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> implements RouteAware {
@override
void didChangeDependencies() {
super.didChangeDependencies();
_routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void dispose() {
_routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPop() {
print('Screen 1: didPop()');
}
@override
void didPopNext() {
print('Screen 1: didPopNext()');
}
@override
void didPush() {
print('Screen 1: didPush()');
}
@override
void didPushNext() {
print('Screen 1: didPushNext()');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Screen 1'),
),
);
}
}
class Screen2 extends StatefulWidget {
@override
_Screen2State createState() => _Screen2State();
}
class _Screen2State extends State<Screen2> implements RouteAware {
@override
void didChangeDependencies() {
super.didChangeDependencies();
_routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void dispose() {
_routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPop() {
print('Screen 2: didPop()');
}
@override
void didPopNext() {
print('Screen 2: didPopNext()');
}
@override
void didPush() {
print('Screen 2: didPush()');
}
@override
void didPushNext() {
print('Screen 2: didPushNext()');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Screen 2'),
),
);
}
}
Output from repro:
flutter: Screen 1: didPush()
flutter: Screen 2: didPush()
Flutter version:
Flutter 1.22.0-10.0.pre.9 • channel master • [email protected]:flutter/flutter.git
Framework • revision 8fa5c55e54 (2 months ago) • 2020-08-30 19:05:20 -0700
Engine • revision 1e302a111d
Tools • Dart 2.10.0 (build 2.10.0-76.0.dev)
Activity
matthew-carroll commentedon Nov 11, 2020
CC @iapicca for any relevant details from previous issue
@Hixie @goderbauer for your history and work with navigation
pedromassangocode commentedon Nov 12, 2020
Reproducible on latest
master.flutter doctor -v
darshankawar commentedon Feb 14, 2023
Verified using latest versions and the reported behavior persists.
runnable code sample
stable, master flutter doctor -v
vasilich6107 commentedon Apr 6, 2025
@darshankawar any progress on this issue|?
Mairramer commentedon Jul 7, 2026
I fixed it, but I need to wait for a few other PRs to be merged before I can put this one up for review.
fix: a page built under a deep link hears didPushNext (flutter/flutte…
Fix RouteObserver didPushNext not firing when routes are pushed synch…