GetxController is not automatically dispose when using Navigator.of(context) for routing

31 views Asked by At

HomePage

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => DetailsPage())),
            child: const Text("To Detail Page")),
      ),
    );
  }
}

DetailsPage

class DetailsPage extends GetView<DetailsController> {
  const DetailsPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(DetailsController());
    return Scaffold(
      appBar: AppBar(
        title: const Text('Details Page'),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Go Back')),
      ),
    );
  }
}

If go to DetailsPage from HomePage using Navigator.of(context).push() the controller is initialized but if I close the DetailsPage through the ElevatedButton or the system back button the controller is not automatically disposed.But If I use Get.to() to navigate to the DetailsPage and close it the controller is disposed automatically. Does controller auto disposal only work with GetX Navigation? How can I dispose controller automatically if I use different routing package?

My Flutter doctor result

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.5, on Microsoft Windows [Version 10.0.19045.4046], locale en-IN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2022.3)
[√] VS Code, 64-bit edition (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 1 category.
0

There are 0 answers