title: Non-nullable instance field must be initialized and Unhandled error LateInitializationError
body: <p>I'm trying to create a database and store an API token for user login but I keep getting an error when the app builds/compiles:</p>
  <blockquote>
  <p>Non-nullable instance field '_database' must be initialized</p>
  </blockquote>
  <p>I've tried adding a late modifier as suggested which then throws:</p>
  <blockquote>
  <p>E/flutter ( 6894): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)]
  Unhandled Exception: Unhandled error LateInitializationError: Field
  '_database@637109567' has not been initialized. occurred in Instance
  of 'AuthenticationBloc'.</p>
  </blockquote>
  <p>my user_db.dart</p>
  <pre><code>import 'dart:io';
  
  import 'package:path/path.dart';
  import 'package:path_provider/path_provider.dart';
  import 'package:sqflite/sqflite.dart';
  
  final userTable = 'userTable';
  
  
  
  class DatabaseProvider {
    static final DatabaseProvider dbProvider = DatabaseProvider();
    //non nullable error on _database
    Database _database;
  
    Future &lt;Database&gt; get database async {
  
      print('before condition');
      if (_database != null){
        return _database;
      }
      _database = await createDatabase();
      return _database;
    }
  
    createDatabase() async {
      Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join(documentsDirectory.path, &quot;User.db&quot;);
  
      var database = await openDatabase(
        path,
        version: 1,
        onCreate: initDB,
        onUpgrade: onUpgrade,
      );
      return database;
    }
  
    void onUpgrade(
        Database database,
        int oldVersion,
        int newVersion,
        ){
      if (newVersion &gt; oldVersion){}
    }
  
    void initDB(Database database, int version) async {
      await database.execute(
          &quot;CREATE TABLE $userTable (&quot;
              &quot;id INTEGER PRIMARY KEY, &quot;
              &quot;username TEXT, &quot;
              &quot;token TEXT &quot;
              &quot;)&quot;
      );
    }
  }
  </code></pre>
  <p>my AuthBloc.dart</p>
  <pre><code>import 'dart:async';
  
  import 'package:bloc/bloc.dart';
  import 'package:meta/meta.dart';
  import 'package:equatable/equatable.dart';
  
  import 'package:times/repository/user_repository.dart';
  import 'package:times/model/user_model.dart';
  
  part 'authentication_event.dart';
  part 'authentication_state.dart';
  
  class AuthenticationBloc
      extends Bloc&lt;AuthenticationEvent, AuthenticationState&gt; {
  
    AuthenticationBloc({required this.userRepository})
        : super(AuthenticationUnauthenticated())
    ;
  
    final UserRepository userRepository;
  
    @override
    Stream&lt;AuthenticationState&gt; mapEventToState(
      AuthenticationEvent event,
    ) async* {
      if (event is AppStarted) {
  
        final bool hasToken = await userRepository.hasToken();
  
        if (hasToken) {
          yield AuthenticationAuthenticated();
        } else {
          yield AuthenticationUnauthenticated();
        }
      }
  
      if (event is LoggedIn) {
        yield AuthenticationLoading();
  
        await userRepository.persistToken(
          user: event.user
        );
        yield AuthenticationAuthenticated();
      }
  
      if (event is LoggedOut) {
        yield AuthenticationLoading();
  
        await userRepository.deleteToken(id: 0);
  
        yield AuthenticationUnauthenticated();
      }
    }
  }
  </code></pre>