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

about controller closing behavior when route closed #818

Closed
liugangnhm opened this issue Nov 25, 2020 · 4 comments
Closed

about controller closing behavior when route closed #818

liugangnhm opened this issue Nov 25, 2020 · 4 comments
Assignees
Labels
expected behavior it is not a bug Improper use of GetX When the issue is caused by the user not using GetX properly Need to insert in the documentation This feature needs to be explained in the documentation.

Comments

@liugangnhm
Copy link

ATTENTION: DO NOT USE THIS FIELD TO ASK SUPPORT QUESTIONS. USE THE PLATFORM CHANNELS FOR THIS. THIS SPACE IS DEDICATED ONLY FOR BUGS DESCRIPTION.
Fill in the template. Issues that do not respect the model will be closed.

Describe the bug
controller closing behavior is not expected for me

Reproduction code
NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)

example:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

class Controller extends GetxController {}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  // Instantiate your class using Get.put() to make it available for all "child" routes there.
  final Controller c = Get.put(Controller());

  @override
  Widget build(context) => Scaffold(
        // Use Obx(()=> to update Text() whenever count is changed.
        appBar: AppBar(title: Text('Home')),

        // Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
        body: Center(
            child: RaisedButton(
          child: Text('to page 1'),
          onPressed: () {
            Get.to(Page1());
          },
        )),
      );
}

class Page1 extends StatelessWidget {
  final Controller controller = Get.put(Controller(), tag: 'page1');

  Page1({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('page1'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('to page2'),
          onPressed: () {
            Get.to(Page2());
          },
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  final Controller controller = Get.put(Controller(), tag: 'page2');

  Page2({Key key}) : super(key: key);

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

To Reproduce
Steps to reproduce the behavior:

  1. click to page 1 button
  2. click to page 2 button
  3. click back
  4. click back

Expected behavior

i put a new Controller in Home, a new Controller(tag:page1) in Page1 , a new Controller(tag:page2) in Page2.

when i pop Page2 to back to page1, i thought Controller(tag:page2) should be close and removed from memory, but with the log i saw, Controller(tag:page2) was not closed. Then same to the Controller(tag:page1).

look at the log:

[GETX] GOING TO ROUTE /Page1
[GETX] "Controllerpage2" has been initialized
[GETX] GOING TO ROUTE /Page2
[GETX] CLOSE TO ROUTE /Page2
[GETX] CLOSE TO ROUTE /Page1
[GETX] "Controllerpage2" onClose() called
[GETX] "Controllerpage2" deleted from memory

Controller closing has one delay to route closing.

Is that expected?

Screenshots
If applicable, add screenshots to help explain your problem.

Flutter Version:

E:\Dev\study\dart\getx_demo>flutter doctor -v
[√] Flutter (Channel beta, 1.24.0-10.2.pre, on Microsoft Windows [Version 10.0.16299.1127], locale zh-CN)
    • Flutter version 1.24.0-10.2.pre at E:\Dev\flutter
    • Framework revision 022b333a08 (7 days ago), 2020-11-18 11:35:09 -0800
    • Engine revision 07c1eed46b
    • Dart version 2.12.0 (build 2.12.0-29.10.beta)
    • Pub download mirror https://mirrors.tuna.tsinghua.edu.cn/dart-pub
    • Flutter download mirror https://mirrors.tuna.tsinghua.edu.cn/flutter

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at E:\Dev\Android\SDK
    • Platform android-30, build-tools 30.0.1
    • Java binary at: E:\Dev\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.0)
    • Android Studio at E:\Dev\Android\Android Studio
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Ultimate Edition (version 2020.1)
    • IntelliJ at E:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1
    • Flutter plugin version 51.0.2
    • Dart plugin version 201.7223.99

[√] IntelliJ IDEA Ultimate Edition (version 2020.3)
    • IntelliJ at E:\Program Files\JetBrains\IntelliJ IDEA 203.5784.10
    • Flutter plugin version 51.0.4
    • Dart plugin version 203.5784.10

[√] VS Code, 64-bit edition (version 1.51.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.16.0

[√] Connected device (3 available)
    • BLA AL00 (mobile) • 5EF7N18404002370 • android-arm64  • Android 10 (API 29)
    • Web Server (web)  • web-server       • web-javascript • Flutter Tools
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 87.0.4280.66

• No issues found!

Getx Version:
get:
git:
url: https://github.com/jonataslaw/getx.git
ref: dev

Describe on which device you found the bug:
ex: Moto z2 - Android.

Minimal reproduce code
Provide a minimum reproduction code for the problem

@eduardoflorence
Copy link
Collaborator

eduardoflorence commented Nov 25, 2020

Hi @liugangnhm,

This is expected behavior. You must place Get.put as the first line of the build method. This is because the flutter executes the code before the buildmethod very quickly, even before the navigation takes place. See an example:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

class Controller extends GetxController {}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(context) {
    final Controller c = Get.put(Controller());

    return Scaffold(
      // Use Obx(()=> to update Text() whenever count is changed.
      appBar: AppBar(title: Text('Home')),

      // Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
      body: Center(
        child: RaisedButton(
          child: Text('to page 1'),
          onPressed: () {
            Get.to(Page1());
          },
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final Controller controller = Get.put(Controller(), tag: 'page1');

    return Scaffold(
      appBar: AppBar(
        title: Text('page1'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('to page2'),
          onPressed: () {
            Get.to(Page2());
          },
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final Controller controller = Get.put(Controller(), tag: 'page2');

    return Scaffold(
      appBar: AppBar(
        title: Text('page2'),
      ),
    );
  }
}

If you use named routes this is not necessary.

@eduardoflorence eduardoflorence added expected behavior it is not a bug Improper use of GetX When the issue is caused by the user not using GetX properly Need to insert in the documentation This feature needs to be explained in the documentation. labels Nov 25, 2020
@liugangnhm
Copy link
Author

Thanks for your quick reply. As you said, what happened when the Widget is rebuilt? multi-controller will be put into? I will have a try now.

@idileepd
Copy link

idileepd commented Dec 25, 2020

Here it is clearly explained
#384
#384 (comment)

@liugangnhm
Copy link
Author

fixed I will close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expected behavior it is not a bug Improper use of GetX When the issue is caused by the user not using GetX properly Need to insert in the documentation This feature needs to be explained in the documentation.
Projects
None yet
Development

No branches or pull requests

4 participants