Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix "Future isn't a type" error in flutter?

I'm trying to store app data of my flutter application in the database, but the complier keeps showing "Future isn't a type" for async func and underlines in red. I've tried removing .analysis-driver as well, but that doesn't help. How can I fix it ?

Code:

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);
  }

 }
like image 350
Ayush Nanglia Avatar asked Dec 04 '22 17:12

Ayush Nanglia


2 Answers

Faced this problem yesterday. Tried a couple of suggestions for fixing this error and today it worked by doing the following:

  1. Update your SDK version in your pubspec.yaml to at least 2.7.0 (Before that my sdk version was 2.2.0 and I think that caused the error. At least 2.7.0 worked for me).

    environment:
      sdk: ">=2.7.0 <3.0.0"
    
  2. Run flutter clean

  3. Go to File -> Invalidate Caches/Restart and select "Invalidate Caches and Restart"

like image 85
niggels Avatar answered Dec 17 '22 09:12

niggels


In my case I was missing a function name, and it gave error in many positions in many files. For example, instead of:

Future<bool> myFunction (int parm1)

accidentally deleted the function name, so the result was:

Future<bool> (int parm1)

This gave problem in many files of the project, giving the error "Future isn't a type". In the errors list, there was also the error of the missing function name, so correcting that one all other errors disappeared.

like image 37
Fabio Pagano Avatar answered Dec 17 '22 10:12

Fabio Pagano