skip to Main Content

class SplashServices {
  UserPreferences userPreferences = UserPreferences();

  void isLogin() {
    userPreferences.getUser().then((value) {
      if (value.token!.isNotEmpty && value.token.toString() != 'null') {
        Timer(
            const Duration(seconds: 5), () => Get.toNamed(RouteName.homeView));

      } else {
        Timer(
            const Duration(seconds: 5), () => Get.toNamed(RouteName.loginView));

      }
    });
  }
}

Since I am facing the problem of using null check operator on a null value
So
Kindly provide an alternate method to solve it.

2

Answers


  1. In your "If" statement, first check "value" is null or not and then do other checks. Do this (if(value==null)) first

    Login or Signup to reply.
  2. Here is issue is you check "null"

    class SplashServices {
      UserPreferences userPreferences = UserPreferences();
    
      void isLogin() {
        userPreferences.getUser().then((value) {
             // Change only the conditions below
          if (value != null && value.token != null) {
            Timer(
                const Duration(seconds: 5), () => Get.toNamed(RouteName.homeView));
    
          } else {
            Timer(
                const Duration(seconds: 5), () => Get.toNamed(RouteName.loginView));
    
          }
        });
      }
    }
    

    Happy Coding…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search