Flutter + SharedPreferences: a build function returned null

190 Views Asked by At

I'm trying to get access to the SharedPreferences and create a permanent file named "first_run".

At the first start of the application it should return "true" and then change it to false.

I declared a future function that return true or false based on that.

Now i got a Wrapper() widget that shows either Loading... , the HomeScreen() or the LoginScreen() based on the result of the future function.

Why is it that the build function returns null ? How can I avoid the "first_run" to get deleted when I update the app ?

Here's the code:

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../screens/home_screen.dart';
import '../screens/login_screen.dart';
import '../providers/auth_provider.dart';

class Wrapper extends StatefulWidget {
  @override
  _WrapperState createState() => _WrapperState();
}

class _WrapperState extends State<Wrapper> {
  FirebaseAuth auth = FirebaseAuth.instance;

  @override
  void initState() {
    AuthProvider().isUserLoggedIn();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    FutureBuilder(
      future: hasAlreadyStarted(),
      builder: (context, snapshot) {
        if (snapshot.hasData == true) {
          return LoginScreen();
        } else {
          return CircularProgressIndicator(
            backgroundColor: Colors.deepOrange,
          );
        }
      },
    );
  }

  Future <bool> hasAlreadyStarted() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    if (prefs.getBool("first_run") == true) {
      prefs.setBool("first_run", false);
      return true;
    } else {
      return false;
    }
  }
}

2

There are 2 best solutions below

0
On BEST ANSWER

You need return keyword

@override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: hasAlreadyStarted(),
      builder: (context, snapshot) {
        if (snapshot.hasData == true) {
          return LoginScreen();
        } else {
          return CircularProgressIndicator(
            backgroundColor: Colors.deepOrange,
          );
        }
      },
    );
  }
0
On

Apart from the fact that you need a return, (which the other answer already pointed out), you should also not produce a new Future every time the build method is called.

 Future<bool> _yourFuture;

 @override
  void initState() {
    AuthProvider().isUserLoggedIn();

    super.initState();

    _yourFuture = hasAlreadyStarted(); // <= start it once
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _yourFuture, // <= reference it for every build