这个 [ int = int? ] 我知道这是一个错误,
但是 [ FutureOr
我不明白这之间的区别
func() 和 func()?
请有人向我解释一下这是怎么回事吗?
这是有问题的代码:
// create a getter to instantiate the database obj
Future<Database> get database async {
// initialize the database when uninitialized
if (_database != null) return _database!;
_database = await _initDB('students.db');
return _database!;
}
Future<Database> _initDB(String filePath) async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, filePath);
return await openDatabase(path, version: 1, onCreate: _createDB);
}
FutureOr<void> _createDB(Database db, int version) async {
const String idType = 'INTEGER PRIMARY KEY AUTOINCREMENT';
const String textType = 'TEXT NOT NULL';
const String boolType = 'BOOLEAN NOT NULL';
const String intType = 'INTEGER NOT NULL';
const String telephoneType = 'INTEGER NOT NULL';
const String studentID = 'StudentId';
const String studentFirstName = 'studentFirstName';
const String studentMiddleName = 'studentMiddleName';
const String studentLastName = 'studentLastName';
const String studentAge = 'studentAge';
const String studentGender = 'studentGender';
const String studentHometown = 'studentHometown';
const String studentLocation = 'studentLocation';
return await db.execute('''
CREATE TABLE Students(
$studentID $idType,
$studentFirstName $textType,
$studentMiddleName $textType,
$studentLastName $textType,
$studentAge $intType,
$studentGender $textType,
$studentHometown $textType,
$studentLocation $textType,
)
''');
}
这也是为了更好的语法突出显示的图像
我相信你必须将这些返回类型更改为可为空
Database?
Future<Database?> get database async {
// initialize the database when uninitialized
if (_database != null) return _database!;
_database = await _initDB('students.db');
return _database!;
}
Future<Database?> _initDB(String filePath) async {
final dbPath = await getDatabasesPath();
final path = join(dbPath, filePath);
return await openDatabase(path, version: 1, onCreate: _createDB);
}
我相信你必须更改 _createdDB,它的返回类型是 Future 并且你返回 db.execute 函数。
FutureOr<void> _createDB(Database db, int
version) async {
等待 db.execute(''' 创建表学生( $studentID $idType, $studentFirstName $textType, $studentMiddleName $textType, $studentLastName $textType, $studentAge $intType, $studentGender $textType, $studentHometown $textType, $studentLocation $textType, ) ''');