Getx: bottomSheet not disposing controller when they are closed

Created on 27 Feb 2021  Â·  4Comments  Â·  Source: jonataslaw/getx

Description
Get.bottomSheet not calling OnClose

Reproduction code

void main() => runApp(MaterialApp(home: Home()));

class Home extends StatelessWidget {
  @override
  Widget build(context) => Scaffold(
        appBar: AppBar(title: Text("Demo")),
        body: Center(
          child: GestureDetector(
            child: Text('open Modal'),
            onTap: () => Get.bottomSheet(ModalWidget()),
          ),
        ),
      );
}

class ModalWidget extends StatelessWidget {
    final ModalWidgetController controller = Get.put(ModalWidgetController());
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      height: 300,
      width: double.infinity,
      child: Text('super cool text!'),
    );
  }
}

class ModalWidgetController extends GetxController {
  @override
  void onInit() {
    print('onInit');
    super.onInit();
  }

  @override
  void onClose() {
    print('onClose');
    super.onClose();
  }
}

To Reproduce

  1. click on 'open Modal';
  2. a red modal should appear;
  3. a 'onInit' print should appear on console;
  4. click away from the red modal, this should close the red modal;
  5. the onClose was not called;

Expected behavior
Call onClose on the controller, and then dispose the controller

Flutter Version:

Flutter 1.22.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 1aafb3a8b9 (4 months ago) • 2020-11-13 09:59:28 -0800
Engine • revision 2c956a31c0
Tools • Dart 2.10.4

Getx Version:
tested on 3.25.6 and 3.23.1

Describe on which device you found the bug:
Pixel 2 (API27)

Most helpful comment

thx for the workaround!
but the Get.bottomSheet() not disposing the controller whitout GetX<Controller> still a issue? or this behavior is expected?

All 4 comments

Instead putting this line:
final ModalWidgetController controller = Get.put(ModalWidgetController());
You can try to use the GetX widget for initiate your controller.
image

Or you can even use the GetView abstract class for instantiate your controller aswell
image

Please try both approach and tell me if still don't work

Hey,
first attempt does not work because i'm not using any observable variable:
[Get] the improper use of a GetX has been detected.You should only use GetX or Obx for the specific widget that will be updated.If you are seeing this error, you probably did not insert any observable variables into GetX/Obx

class ModalWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<ModalWidgetController>(
      init: ModalWidgetController(),
      builder: (_) => Container(
        color: Colors.red,
        height: 300,
        width: double.infinity,
        child: Text('super cool text!'),
      ),
    );
  }
}
class ModalWidgetController extends GetxController {
  @override
  void onInit() {
    print('onInit');
    super.onInit();
  }
  @override
  void onClose() {
    print('onClose');
    super.onClose();
  }
}

and the second try, the onInit and onClose is not even called :

class ModalWidget extends GetView<ModalWidgetController> {
  @override
  Widget build(BuildContext context) {
    return  Container(
        color: Colors.red,
        height: 300,
        width: double.infinity,
        child: Text('super cool text!'),
    );
  }
}
class ModalWidgetController extends GetxController {
  @override
  void onInit() {
    print('onInit');
    super.onInit();
  }
  @override
  void onClose() {
    print('onClose');
    super.onClose();
  }
}

You found the reason. The controller is just initiate if you are using something from it.

Make a test, try to put some var in controller and use that on your screen, you will the circle working correctly

thx for the workaround!
but the Get.bottomSheet() not disposing the controller whitout GetX<Controller> still a issue? or this behavior is expected?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nipodemos picture Nipodemos  Â·  4Comments

omartinma picture omartinma  Â·  3Comments

rupamking1 picture rupamking1  Â·  3Comments

williamsilva-98 picture williamsilva-98  Â·  4Comments

Denilson-source picture Denilson-source  Â·  3Comments