jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.39k stars 1.63k forks source link

Controller not disposing off automatically when going back to previous screen. #1836

Open akshg05 opened 3 years ago

akshg05 commented 3 years ago

Describe the bug I have a minimlaist sample app running on Android with GetX as State Management lib only. There are two screens LandingPage and MainScreen. On going back from MainScreen to LandingPage screen, the controller is not autodisposing as expected.

To Reproduce Steps to reproduce the behavior:

  1. Press the button to go to Main Screen
  2. Press add counter button to increment to some value
  3. Press back button to go to prev screen
  4. Go to MainScreen again using button
  5. The counter retains the existing value instead of being 0

Expected behavior The counter value should be 0

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

Flutter Version: 2.2.3

Getx Version: latest 4.3.8

Describe on which device you found the bug: ex: Android - Pixel emulator

Minimal reproduce code

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.purple,
      ),
      home: LandingScreen(),
    );
  }
}

class LandingScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue[800],
      child: Center(
        child: ElevatedButton(
          onPressed: () => {
            Navigator.of(context)
                .push(MaterialPageRoute(builder: (_) => MainScreen()))
          },
          child: const Text('Navigate to Second Screen'),
        ),
      ),
    );
  }
}

class MainScreen extends StatelessWidget {
  final MyController controller = Get.put(MyController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          color: Colors.blueAccent,
          child: Center(
            child: Column(
              children: [
                Obx(() => Text('Clicked ${controller.count}')),
                FloatingActionButton(
                  onPressed: controller.increment,
                  child: Text('+'),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class MyController extends GetxController {

  var count = 0.obs;

  void increment() => count++;

}
akshg05 commented 3 years ago

I tried using the GetX navigaton as well with no luck :/

rawquesh commented 3 years ago

declare controller in build function.

Saeeed-B commented 2 years ago

not working

WorkWithAfridi commented 2 years ago

My solution would be to delete the controller on widget dispose. And Of course for that, I would need to use StatefulWidget. @override void dispose() { Get.delete(); super.dispose(); }