Meggy
Meggy

Reputation: 1671

Flutter/Dart - FutureBuilder bool not returning bool

I've got a boolean provided by ChangeNotifierProvider which grabs true or false from SharedPreferences. It should update to "true" once a user has logged in. I'm calling this boolean from a FutureBuilder. However, instead of getting the correct boolean, all I'm getting is the CircularProgressIndicator() indicating something has gone wrong. Here's the code;

Widget build(BuildContext context) {
        var socialProvider = Provider.of<SocialProvider>(context);
        return FutureBuilder<bool>(
          future: socialProvider.loadCurrentLogged(),
          builder: (context, snapshot){
            if(!snapshot.hasData) {
              return
                Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Consumer<SocialProvider>(
                          builder: (context, socialProvider, _) =>
                              Container(
                                child: new RaisedButton(
                                  onPressed: () {
                                    if (snapshot.data != true) {
                                      //Do something here.
                                    } else { // } //switch end
                                  }
                                 ),
                              ),
                           ),
                        ),
                      ],
                    ),
                  ],
                );
            } else {return
              Center(
                  child: CircularProgressIndicator()
              );
            }
          },
        );
      }

And here's the future provided by the ChangeNotifierProvider;

 Future<bool> loadCurrentLogged() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool currentlogged = prefs.getBool('currentlogged') ;
    return currentlogged;
  }

What's wrong?

Upvotes: 1

Views: 1796

Answers (1)

Meggy
Meggy

Reputation: 1671

Doh!

if(!snapshot.hasData) 

should be;

if(snapshot.hasData)

Sigh...

Upvotes: 4

Related Questions