dart 无法将参数类型“FutureOr < void>Function(Database,int)”分配给参数类型“FutureOr < void>Function(Database,int)?”

ego6inou  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(126)


的数据
这个[ int = int?]我知道这是一个错误,
但是[ FutureOr func()= FutureOr func()?]
我不明白
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,
      )
      ''');
    }

字符串
这里的图像也更好的语法突出显示


yvfmudvl

yvfmudvl1#

我相信你必须改变_createdDB,它的返回类型是Future<void>,你返回db.execute函数。只需删除return语句。

FutureOr<void> _createDB(Database db, 
   int version) async {  
   await db.execute('''
      CREATE TABLE  Students(
        $studentID $idType,
        $studentFirstName $textType,
        $studentMiddleName $textType,
        $studentLastName $textType,
        $studentAge $intType,
        $studentGender $textType,
        $studentHometown $textType,
        $studentLocation $textType,
      )
      ''');

字符串

rryofs0p

rryofs0p2#

我相信你必须将这些返回类型改为可空的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);
       }

字符串

相关问题