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
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)
Instead putting this line:
final ModalWidgetController controller = Get.put(ModalWidgetController());
You can try to use the GetX widget for initiate your controller.

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

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?
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?