I'm using GetX for state management in a Flutter web application. I have created an authcontroller with a login method. When I call the method anywhere in the application it works and the UI changes with the new state. But when I refresh explorer the controller is reset and login state is lost.
I have created a small version of the code easy to reproduce:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp( MyApp());
}
class MyApp extends StatelessWidget {
AuthController authController = Get.put(AuthController(), permanent: true);
MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
smartManagement: SmartManagement.keepFactory,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
HomeScreen({Key? key}) : super(key: key);
final AuthController authController = Get.find();
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Obx(() => authController.isAuthenticated.value ? Text("authenticated") : Text("authenticate")),
onPressed: () {
authController.login();
},
)
);
}
}
class AuthController extends GetxController {
var isAuthenticated = false.obs;
void login() {
isAuthenticated.value = true;
update();
}
}
As you can see I'm using the permanent:true
prop but the controller still is re-initialized.
This is how the issue looks:
Is there any prop or config that I'm missing? how to avoid this behavior?