why does this Dart have issue with return class variable, but can return a copy of it? Error message: "A value of type 'Database?' can't be returned from the function 'database' because it has a return type of 'Future'."
Note this question is focused on why the issue does not arise for Scenario 1. (i.e. question is NOT why Scenario 2 has an issue, but rather why Scenario 1 does not seem to have the issue)
import 'package:sqflite/sqflite.dart';
class DatabaseHelper {
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
DatabaseHelper._privateConstructor();
Future<Database> get database async {
// Scenario 1 - Works
Database? testDb = _database;
if (testDb != null) return testDb;
// Scenario 2 - Does NOT work??
if (_database != null) return _database; // "A value of type 'Database?' can't be returned from the function 'database' because it has a return type of 'Future<Database>'."
Database? instance = await _initDatabase();
_database = instance;
return instance;
}
Future<Database> _initDatabase() async {
Database _db = await openDatabase('my_db.db');
return _db;
}
}
Image