Flutter + SharedPreferences: a build function returned null

Issue

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;
    }
  }
}

Solution

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,
          );
        }
      },
    );
  }

Answered By – chunhunghan

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *