How do I fix "Future not a type" error in flutter?

205

You need to change the return type of your onCreate function. It should be void instead of Future.

void _onCreate(Database db, int version) async{
  await db.execute('''
  CREATE TABLE $table (
  $task_id INTEGER PRIMARY KEY,
  $task TEXT NOT NULL,
  $bool_check INTEGER)
  ''');
}
Share:
205
Ayush Nanglia
Author by

Ayush Nanglia

Updated on December 25, 2022

Comments

  • Ayush Nanglia
    Ayush Nanglia over 1 year

    I was creating a database in flutter and I unknowingly added ~/.dartServer/.analysis-driver/ to my program. The complier now shows "Future not a type" with red line beneath. Here is the code for ref :

    import 'package:sqflite/sqflite.dart';
    import 'package:path_provider/path_provider.dart';
    import 'package:path/path.dart';
    import 'dart:io';
    
    class Db_Help{
    
      static final _db_name="Work_tasks.db";
      static final _db_version=1;
      static final table="Work Table";
    
      static final task_id="ID";
      static final task="Task";
      static final bool_check="Value";
    
      static Database _database;
    
      Db_Help._private_cons();
      static final Db_Help instance=Db_Help._private_cons();
    
      _initDatabase() async{
        Directory docu_dir=await getApplicationDocumentsDirectory();
        String path=join(docu_dir.path,_db_name);
    
        return await openDatabase(path, version: _db_version, onCreate: _onCreate);
      }
    
      Future<Database> get database async{
    
        if(_database!=null)
          return _database;
    
        _database=await _initDatabase();
        return _database;
    
      }
    
      Future _onCreate(Database db, int version) async{
        await db.execute('''
        CREATE TABLE $table (
        $task_id INTEGER PRIMARY KEY,
        $task TEXT NOT NULL,
        $bool_check INTEGER)
        ''');
      }
    
      Future<int> insert(Map<String,dynamic> row) async{
        Database db= await instance.database;
        return await db.insert(table, row);
      }
    
     }
    

    Can Someone help in removing ~/.dartServer/.analysis-driver/ (which is probably the root cause) ? I don't know how to remove that.

    Thanks in advance :)

  • Ayush Nanglia
    Ayush Nanglia over 3 years
    Nope, doesn't help