Getx controller cant dispose

532 Views Asked by At

i create a function to cek user, from API to check user login or not. i use this because my API is not realtime. this my code for funcheckuser

void funfetchUser() async {
  print('get user');
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var token = prefs.getString('token');
  try {
    var headers = {
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    };
    var url = Uri.parse(Api.base + Api.user);
    https.Response response = await https.get(url, headers: headers);
    if (response.statusCode == 200) {
      var user = jsonDecode(response.body)['user'];
      var isLogin = token;
      print(isLogin);
      UserList.email = user['email'];
      UserList.userlongname = user['user_longname'];
      UserList.userphone = user['user_phone'];
      
      Future.delayed(Duration(seconds: 1), (() {
        if (isLogin == null) {
          Get.toNamed('/login');
        } else {
          print('stay');
        }
      }));
      
    } else {
      var login2 = jsonDecode(response.body);
      print(login2);
      // customAllertDialog("Sesi", 'Sesi Berakhir', 'error');
      print('logout');
      prefs.clear();
      Get.toNamed('/login');
    }
  } catch (e) {
    print(e);
    prefs.clear();
  }
}

I create this fun on to all my controller

@override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    print('ini profile');
    funfetchUser();
  }
  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    Timer.periodic(Duration(seconds: 
    30), (timer) { 
      print('cek user profile gan');
      funfetchUser();
    });
  }
  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    Get.delete<ProfilController>();
  }

that controller always run even i move page. i want to call every controller in every page but the one n only controller run is contoller profile

1

There are 1 best solutions below

1
On

The removal or not of the controllers in the Getx package is related to routing

And you should not delete controllers manually