I am using the following package https://pub.dev/packages/get. Do I need to close my .obs in the onClose of a GetxController? I can't find anything about this in the docs. And looking at my memory it appears that the are being destroyed automatically.
Are obs stream being closed automatically by GetxControllers?
5.7k Views Asked by anonymous-dev At
3
There are 3 best solutions below
1

Based from the code of the super implementation of onClose, by default it does nothing currently.
And from the comments, it says:
/// Called before [onDelete] method. [onClose] might be used to
/// dispose resources used by the controller. Like closing events,
/// or streams before the controller is destroyed.
/// Or dispose objects that can potentially create some memory leaks,
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
void onClose() {}
from that I think you need to manually close your streams in YourController::onClose() override function.
0

It appears you can use obs safely when using GetWorkers. Run this code and you'll notice that when you click the buttons a few time there will only be one print per page switch.
void main(){
runApp(GetMaterialApp(home: TestWidget(),));
}
class TestWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('next'),
onPressed: () => Get.to<SomeWidget>(SomeWidget()),
),
);
}
}
class SomeWidget extends StatelessWidget {
RxBool isSubscribed = false.obs;
SomeWidget() {
ever(isSubscribed, (_) => print('test'));
}
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('back'),
onPressed: () {
isSubscribed.value = !isSubscribed.value;
Get.back();
},
),
);
}
}
In my understanding of GetX + Flutter so far...
No, you shouldn't have to remove .obs in the
close()
method of GetxControllers. Disposal of observables from a Controller are done automatically when the Controller is removed from memory.GetX disposes/removes GetxControllers (and their observables) when the widget in which they are contained are popped off the widget stack / removed from the widget tree (by default, but can be overridden).
You can see this in the override of
dispose()
methods of various Get widgets.Here's a snippet of
dispose()
that's run whenGetX
widgets are popped/removed:When you use Bindings or
Get.to()
you're usingGetPageRoute
's which do cleanup by Route names:Test App
Below is a test App you can copy/paste into Android Studio / VSCode and run to watch the debug or run window output for GETX lifecycle events.
GetX will log the creation & disposal of Controllers in and out of memory.
The app has a HomePage and 3 ChildPages using Get Controllers in 3 ways, all which remove itself from memory:
Notes
Get.to vs. Navigator.push
When using
Get.put()
in a child widget be sure you're usingGet.to()
to navigate to that child rather than Flutter's built-inNavigator.push
.GetX wraps the destination widget in a
GetPageRoute
when usingGet.to
. This Route class will dispose of Controllers in this route when navigating away / popping the widget off the stack. If you useNavigator.push
, GetX isn't involved and you won't get this automatic cleanup.Navigator.push
Get.to