'GetxController obs value inside other controller

I want to use LoginController token value inside userController to get the authToken which is an obs value but the value I am getting inside userController is non updated(i.e null which is initially set value).

LoginController

final token = "".obs;

isLoggedIn()async{

var storageToken = await _storage.read(key: "token");

token (storageToken);
print(token.value) //gives a value

}

UserController

var loginController = Get.put(LoginController());

home()async{

var token = loginController.token.value

print(token) // gives empty string

}


Solution 1:[1]

The reason for this is because you initialized again your LoginController by putting this code Get.put(LoginController()) on UserController.

You can approach this in two ways on your UserController:

var loginController = Get.find<LoginController>();
var token = loginController.token.value;

Or

var token = Get.find<LoginController>().token.value;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kei Credo