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

Misaligned routes and dependencies #659

Closed
XuJin186 opened this issue Sep 28, 2020 · 15 comments
Closed

Misaligned routes and dependencies #659

XuJin186 opened this issue Sep 28, 2020 · 15 comments
Assignees
Labels
Improper use of GetX When the issue is caused by the user not using GetX properly

Comments

@XuJin186
Copy link

XuJin186 commented Sep 28, 2020

Describe the bug
When using Get.to() Get uses the current path when registering dependencies, not the path you want to go

To Reproduce
Steps to reproduce the behavior:

  1. Run the project
  2. Click the Demo1 button
  3. void _registerRouteInstance<S>({String tag}) { _routesKey.putIfAbsent(_getKey(S, tag), () => Get.reference); } 检查Get.reference的值
  4. See Get.reference is /, it should be /Demo1

Expected behavior
void _registerRouteInstance<S>({String tag}) { _routesKey.putIfAbsent(_getKey(S, tag), () => Get.reference); }
Get.reference When it should /Demo1
Screenshots
微信截图_20200928092123
微信截图_20200928092157

Flutter Version:
10.0.18363.1082

Getx Version:
get: ^3.11.1

Describe on which device you found the bug:
Emulator

Minimal reproduce code

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  App({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Home(),
    );
  }
}



class Home extends StatelessWidget {
  Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
      ),
      body: Column(
        children: [
          RaisedButton(
            onPressed: () {
              Get.to(Demo1());
            },
            child: Text("Demo1"),
          ),
          RaisedButton(
            onPressed: () {
              Get.to(Demo2());
            },
            child: Text("Demo2"),
          )
        ],
      ),
    );
  }
}


class Demo1Controller extends GetxController {}

class Demo1 extends StatelessWidget {
  final Demo1Controller _demo1controller = Get.put(Demo1Controller());
  Demo1({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Demo1"),
      ),
    );
  }
}

class Demo2Controller extends GetxController {}

class Demo2 extends StatelessWidget {
  final Demo2Controller _demo1controller = Get.put(Demo2Controller());
  Demo2({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Demo2"),
      ),
    );
  }
}
@jonataslaw
Copy link
Owner

jonataslaw commented Sep 28, 2020

The problem it on:

return GetMaterialApp(
      home: Home(), // It's close home instance
    );

As I mentioned in past issues, the MaterialApp home should not receive a Widget, but a widget function.
Because of this Flutter engineering error, when closing a home instance, you are automatically calling the class's constructor, even before the MaterialApp class's constructor.

There are two ways to resolve this:
1: Using a getter instead of a final instance:
Demo1Controller get _demo1controller => Get.put(Demo1Controller());

2: Using named routes. You can use named routes only for the first widget, if you don't want to use named routes for the entire app:

return GetMaterialApp(
      getPages: [GetPage(name:'/': page:()=> Home())],
    );

@XuJin186
Copy link
Author

XuJin186 commented Sep 28, 2020

Thank you for your reply, I have tested the solution you mentioned, and it did not achieve the expected effect

@XuJin186
Copy link
Author

The first one is OK, the second one is not OK

@Nipodemos
Copy link
Collaborator

@XuJin186 Can you tell us exactly what happened when you used the second option?
It was the exact same problem? It was a different one?

@Nipodemos Nipodemos added Unrelated to GetX Issues that the error is not GetX's fault, is from another package. Waiting for customer response labels Oct 10, 2020
@XuJin186
Copy link
Author

@XuJin186 Can you tell us exactly what happened when you used the second option?
It was the exact same problem? It was a different one?

Yes

@Nipodemos
Copy link
Collaborator

@XuJin186 Can you tell us exactly what happened when you used the second option?
It was the exact same problem? It was a different one?

Yes

I made three questions, to which one are you answering yes?

@XuJin186
Copy link
Author

The second solution is the same as the previous error

@XuJin186
Copy link
Author

Because the oral complaint may be unclear, I also provided the test code. Please copy it and run it to see what I said.

@eduardoflorence
Copy link
Collaborator

For me it worked when all routes are named and I replaced Get.to with Get.toNamed. In this case the controllers have been removed from memory

getPages: [
  GetPage(name: '/', page: () => Home()),
  GetPage(name: '/demo1', page: () => Demo1()),
  GetPage(name: '/demo2', page: () => Demo2()),
],

@jonataslaw this seems to be a very serious problem, because when we don't use the named routes, the controllers are not removed from memory in the removeDependencyByRoute method

@eduardoflorence
Copy link
Collaborator

I remembered the solution now, which was already mentioned in another issue:
@XuJin186, you must put Get.put inside the statelessWidget build method:

  @override
  Widget build(BuildContext context) {
    final Demo2Controller _demo1controller = Get.put(Demo2Controller()); // <- here
    return Scaffold(
      appBar: AppBar(
        title: Text("Demo2"),
      ),
    );
  }

@eduardoflorence
Copy link
Collaborator

My answer was based on this comment from @jonataslaw:
#384 (comment)

@XuJin186
Copy link
Author

My answer was based on this comment from @jonataslaw:
#384 (comment)

Thank you very much for your reply

@eduardoflorence
Copy link
Collaborator

You're welcome @XuJin186, can this issue be closed?

@XuJin186
Copy link
Author

You're welcome @XuJin186, can this issue be closed?

I use named routes now, so I don’t know if it can solve the problem. There must be a solution. We are only pursuing the best solution.

@Nipodemos
Copy link
Collaborator

Since both @jonataslaw and @eduardoflorence provided solutions and @XuJin186 started to using them, i'll close this

@Nipodemos Nipodemos added bug Something isn't working Improper use of GetX When the issue is caused by the user not using GetX properly and removed Unrelated to GetX Issues that the error is not GetX's fault, is from another package. Waiting for customer response bug Something isn't working labels Nov 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improper use of GetX When the issue is caused by the user not using GetX properly
Projects
None yet
Development

No branches or pull requests

4 participants