Description
Steps to reproduce
In some scenarios, developers may want to use removeRoute()
instead of pop()
to remove a nested route which is not the first one. By calling removeRoute()
alone, all codes which await a call to push()
will never end : the future will never be resolved.
final Object? result = await Navigator.of(context).push() // By using removeRoute(), this will never end
To fix this issue, developper should maybe call route.didPop(any_data_or_null)
after any removeRoute(route)
call. I didn't know if this solution is 'perfect' and 'enough'. That's why i created this issue : to point an unresolved behavior and get a solution.
Documentation well mention this limitation but didn't provide any solution to release the futures.
My potential solution is to add after Navigator.of(context).removeRoute(route)
a call to route.didPop()
. If i'm right and didPop is not a bad idea, maybe the documentation should be updated to inform developers about this.
- Launch app
- Click on "open test page"
- Click on "Remove current route"
- Observe console logs
Expected results
In logs :
Open test page
Test page is closed: null
Actual results
In logs :
Open test page
Code sample
Code sample
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Home(),
),
);
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ElevatedButton(
onPressed: () async {
print("Open test page");
final Object? result = await Navigator.of(context).push(
MaterialPageRoute(
settings: const RouteSettings(name: TestRoute.routeName),
builder: (BuildContext context) => const TestRoute(),
),
);
print("Test page is closed: $result");
},
child: const Text('Open test page'),
),
],
),
),
),
);
}
}
class TestRoute extends StatelessWidget {
const TestRoute({super.key});
static const String routeName = 'test-route';
Route<dynamic>? currentRoute(BuildContext context) {
Route<dynamic>? result;
Navigator.of(context).popUntil((Route<dynamic> route) {
result = route;
return true;
});
return result;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Test page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
final Route<dynamic> route = currentRoute(context)!;
Navigator.of(context).removeRoute(route);
},
child: const Text('Remove current route'),
),
),
);
}
}
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
Not needed
Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-arm64, locale fr-FR)
• Flutter version 3.24.3 on channel stable at /Users/earminjon/fvm/versions/3.24.3
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 2663184aa7 (6 weeks ago), 2024-09-11 16:27:48 -0500
• Engine revision 36335019a8
• Dart version 3.5.3
• DevTools version 2.37.3
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at /Users/earminjon/Library/Android/Sdk
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = /Users/earminjon/Library/Android/Sdk
• Java binary at: /Users/earminjon/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 16A242d
• CocoaPods version 1.14.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2024.1)
• Android Studio at /Users/earminjon/Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
[✓] Android Studio (version 2023.2)
• Android Studio at /Users/perso/Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
[✓] IntelliJ IDEA Ultimate Edition (version 2024.2.3)
• IntelliJ at /Users/earminjon/Applications/IntelliJ IDEA Ultimate.app
• Flutter plugin version 82.0.3
• Dart plugin version 242.22855.32
[✓] Connected device (4 available)
• sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 15 (API 35) (emulator)
• macOS (desktop) • macos • darwin-arm64 • macOS 15.0.1 24A348 darwin-arm64
• Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.0.1 24A348 darwin-arm64
[✓] Network resources
• All expected network resources are available.
• No issues found!