我通过 flutter upgrade --force
升级了 flutter sdk。该项目在升级前运行良好,但升级后出现错误。我目前正在使用最新的稳定版 flutter。
E/flutter (17687): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'call' was called on null.
E/flutter (17687): Receiver: null
E/flutter (17687): Tried calling: call()
E/flutter (17687): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (17687): #1 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:31:24)
E/flutter (17687): #2 SembastDatabase.flush (package:sembast/src/database_impl.dart:593:27)
E/flutter (17687): #3 SembastDatabase.open (package:sembast/src/database_impl.dart:879:11)
E/flutter (17687): <asynchronous suspension>
E/flutter (17687): #4 DatabaseOpenHelper.openDatabase.<anonymous closure> (package:sembast/src/database_factory_mixin.dart:80:7)
E/flutter (17687): <asynchronous suspension>
E/flutter (17687): #5 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16)
E/flutter (17687): <asynchronous suspension>
E/flutter (17687): #6 DaoDatabase._openDatabase (package:kitabyatra/db/DaoDatabase.dart:43:26)
E/flutter (17687): <asynchronous suspension>
我已经实现如下:
class DaoDatabase {
DaoDatabase();
static final DaoDatabase _singleton = DaoDatabase();
static DaoDatabase get instance => _singleton;
Completer<Database> _dbOpenCompleter;
Future<Database> get database async {
if (_dbOpenCompleter == null) {
_dbOpenCompleter = Completer<Database>();
_openDatabase();
}
return _dbOpenCompleter.future;
}
Future<dynamic> _openDatabase() async {
final Directory appDocumentDir = await getApplicationDocumentsDirectory();
final String dbPath = join(appDocumentDir.path, Strings.appDbName);
final Database database = await databaseFactoryIo.openDatabase(dbPath); //--> Getting error here
_dbOpenCompleter.complete(database);
}
}