0

I am trying to check the login of the user. But, checklogin() even on returning null doesn't equate to null in the if condition.

class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        if(checkLogin() == null) {
          return Login();
        } else {
          return Dashboard();
        }
      }
      Future<String> checkLogin() async {
        var prefs = await SharedPreferences.getInstance();
        var key = 'Token';
        var value = prefs.getString(key);
        print(value);  
        return value;
      }
    }

2 Answers 2

2

I just used a future builder which manages the data returned from future through AsyncSnapshot.

class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {

       return FutureBuilder(
        future: checkLogin(),
        builder: (BuildContext context, AsyncSnapshot snapshot){


        if (snapshot.hasData){
           var value = snapshot.data;
           if(value == null){
              return Login();
             }else{
              return Dashboard();
             }
        }else return Dashboard();
      }
    );
  }




      Future<String> checkLogin() async {
        var prefs = await SharedPreferences.getInstance();
        var key = 'Token';
        var value = prefs.getString(key);
        print(value);  
        return value;

      }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

check out my update, it might have syntax error I just wrote it down here
It's giving an error, the build function returned null.
I noticed it directly returns from build just now, in that case you will have to use a FutureBuilder. I will be writing the code now
Is there any other method that doesn't returns from the build?
you can use IndexedStack which shows only one page and contains multiple pages as stack, if you change the index you change the page. Then until your data loads show whatever you want like centered loading screen, then when the data finishes loading you change the state and the index of the stack will return to the page you want, now this problem is build needs to show something until it waits for the future to return.
|
0

The issue is checkLogin function is async and return Future for which you'll have wait and you wait in build directly.

So, here is a better and correct implementation. ``

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool isLogin = false;

  @override
  void initState() {
    checkLogin();
    super.initState();
  }

  checkLogin() async {
    var prefs = await SharedPreferences.getInstance();
    var key = 'Token';
    var value = prefs.getString(key) ?? false;
    setState(() {isLogin = value;});
  }

  @override
  Widget build(BuildContext context) {
    return isLogin ? Login() : Dashboard();
  }
}

``

1 Comment

Updated the answer. Check it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.