why static Database _database; and DatabaseHelper._privateConstructor(); gives an error?

Viewed 854
import 'package:sqflite/sqflite.dart';

class DatabaseHelper{
  static final _dbName= 'myDatabase.db';
  static final _dbVersion=1;
  DatabaseHelper._privateConstructor();
  static final DatabaseHelper instance=DatabaseHelper._privateConstructor();
  static Database _database;

  Future <Database> get database async{
    if(_database!=null)return _database;

        return _database;
  }
}

why static Database _database; and DatabaseHelper._privateConstructor(); gives an error???

1 Answers

change

static Database _database;

to

static Database? _database;

because of null safety feature (recent update in flutter).

And also you will need to change

Future <Database> get database async{
    if(_database!=null)return _database;
       return _database;
  }

to

Future <Database> get database async{
    return _database ??= await initDB();
}

initDB() is a function to initialise the database.

Related