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. I am using Flutter's Navigation only without wrapping with GetMaterialApp.
My expectation is that the value exposed by the controller should be reset to its initial value when the Controller is instantiated. However, the Widget continues to show the last value from the controller.
I am using the latest version of Flutter and GetX as avail of now : 2.2.3 and 4.3.8 respectively
Your help is appreciated.
Code:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
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: () => {
Get.to(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('+'),
),
ElevatedButton(
onPressed: ()=>{Navigator.of(context).pop()},
child: Text('Go Back'),
)
],
),
),
),
),
);
}
}
class MyController extends GetxController {
var count = 0.obs;
void increment() => count++;
}