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

GetX 5: initialRoute parameter of GetMaterialApp() is ignored, if a route with name '/' is registered #3196

Open
thomasgi1 opened this issue Aug 29, 2024 · 0 comments
Assignees

Comments

@thomasgi1
Copy link

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:

  1. Run the app in debug mode
  2. Page two should be possible, but page one is shown
  3. Click on 'next screen' button
  4. 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 5db4322

Describe on which device you found the bug:
MacOS

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