GetX 5: initialRoute parameter of GetMaterialApp() is ignored, if a route with name '/' is registered
Describe the bug When you register a page with name '/', then the initialRoute parameter of the GetMaterialApp() constructor is ignored. Furthermore no navigation to another page than the '/' page is possible.
**Reproduction code
example:
Change the example app in getx repository as follows:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialRoute: '/second',
getPages: [
GetPage(
participatesInRootNavigator: true,
name: '/',
page: () => const First()),
GetPage(
name: '/second',
page: () => const Second(),
transition: Transition.downToUp,
),
GetPage(
name: '/third',
page: () => const Third(),
),
GetPage(
name: '/fourth',
page: () => const Fourth(),
),
],
debugShowCheckedModeBanner: false,
);
}
}
class FirstController extends GetxController {
@override
void onClose() {
print('on close first');
super.onClose();
}
}
class First extends StatelessWidget {
const First({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
print('First rebuild');
Get.put(FirstController());
return Scaffold(
appBar: AppBar(
title: const Text('page one'),
leading: IconButton(
icon: const Icon(Icons.more),
onPressed: () {
Get.snackbar(
'title',
"message",
mainButton:
TextButton(onPressed: () {}, child: const Text('button')),
isDismissible: true,
duration: Duration(seconds: 5),
snackbarStatus: (status) => print(status),
);
// print('THEME CHANGED');
// Get.changeTheme(
// Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
},
),
),
body: Center(
child: SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.toNamed('/second?id=123');
},
child: const Text('next screen'),
),
),
),
);
}
}
class SecondController extends GetxController {
final textEdit = TextEditingController();
@override
void onClose() {
print('on close second');
textEdit.dispose();
super.onClose();
}
}
class Second extends StatelessWidget {
const Second({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final controller = Get.put(SecondController());
print('second rebuild');
return Scaffold(
appBar: AppBar(
title: Text('page two ${Get.parameters["id"]}'),
),
body: Center(
child: Column(
children: [
Expanded(
child: TextField(
controller: controller.textEdit,
)),
SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.toNamed('/third');
},
child: const Text('next screen'),
),
),
],
),
),
);
}
}
class Third extends StatelessWidget {
const Third({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: const Text('page three'),
),
body: Center(
child: SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.offNamedUntil('/fourth', (route) {
return Get.currentRoute == '/first';
});
},
child: const Text('go to first screen'),
),
),
),
);
}
}
class Fourth extends StatelessWidget {
const Fourth({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: const Text('page four'),
),
body: Center(
child: SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.back();
},
child: const Text('go to first screen'),
),
),
),
);
}
}
To Reproduce Steps to reproduce the behavior:
- Run the app in debug mode
- Page two should be possible, but page one is shown
- Click on 'next screen' button
- No page change happens
Expected behavior The initial route should be respected even if one of the registered pages has the route '/'.
Flutter Version: 3.24.1
Getx Version: git commit 5db4322c417cf7de10077c79dfa5cc6c5259a465
Describe on which device you found the bug: MacOS
I also encountered this issue on Flutter version 4.6.6, but it only happens when the app is opened via a deeplink or a notification. One thing I’ve noticed is that this issue does not occur when a colleague builds the app using his Mac — it only happens when I build the app on my machine.
Any Solution??
I found something on my side:
-
flutter run --release→ ❌ Bug happens -
flutter run --debug→ ❌ Bug happens -
flutter run --profile→ ❌ Bug happens -
flutter build apk --release→ ❌ Bug happens -
flutter build apk --debug→ ❌ Bug happens -
flutter build apk --profile→ ❌ Bug happens
✅ But when I click the Run button in Android Studio, the bug does not happen