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

23k Views Asked by At

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

 }
16

There are 16 best solutions below

0
On BEST ANSWER

I saw this problem last week. Here is what you can do, moving in that order if not fixed:

  1. Make sure you have the dart:async import

  2. Make sure your project level sdk is set correctly

File -> Project Structure In Project, select valid Project SDK. In Modules -> myapp_android -> Dependencies, select a valid Module SDK.

  1. Update the SDK version in your pubspec.yaml

  2. Flush your cache and restart Android Studio.

File -> Invalidate Caches/Restart and select "Invalidate Caches and Restart"

1
On

If you have recently upgraded flutter maybe that is causing an issue.

I too faced it once after an upgrade, however a simple IDE restart solved the issue for me. Maybe you can try that.

You can have a look at this thread that I found here.

Do let me know if it solves the issue.

2
On

I faced this problem several time before. For my case...

Go to File -> Invalidate Caches / Restart... -> Invalidate and Restart

1
On

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"

0
On

Close the ide and reopen it, It worked

0
On

A lot of times it happens because of some other compilation error in the code, unrelated to futures.

Just try to solve other compilation problems and the error may go away.

0
On

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.

0
On

I faced this problem by using vs code. I fixed the problem by closing and reopening the vs code .

0
On

I was facing the same issue after upgrading my flutter sdk version. The problem was that my flutter sdk path was set to the newer one but the dart sdk path wasn't updated automatically (Which is usually done).

Here is how I resolved it:

  1. Go to Settings>Languages & Frameworks>Dart
  2. Set the dart SDK path of the newly downloaded sdk which is [pathToYourNewFlutterSdk]/bin/cache/dart-sdk.
  3. Press Apply and Ok and you are good to go.
  4. Your IDE may need a restart sometimes to take the effect.

Make sure you're using the dart-sdk path of the same flutter-sdk you're using.

0
On

after updating flutter version, i faced this issue.

solved by just close vs code completely and reopen it. worked for me!!!.

0
On

Issue affecting upgrade of Flutter/Dart SDK

Restart your IDE (Visual Studio Code / Android Studio) on the project base run:

flutter clean

flutter pub get

Ensure import 'dart:async' is present to use future

0
On

I faced the same issue but for me, it was the version of Dart that was referenced by Ide. I was using fvm for flutter version control and by defining the flutter version at my setting.json have resolved this.

"dart.flutterSdkPath": ".fvm/flutter_sdk",
0
On

it's probably because you upgraded flutter . you need to change sdk version in your yaml file. to get correct sdk version create new project and check its sdk version in yaml file , the copy it to your project then invalidate cache and restart IDE.

0
On

In my case, I was using fvm for flutter version control and Android Studio for IDE. The problem was solved by defining the fvm flutter sdk in the android sudio project sdk path.

In Android Studio:

  • Go to Languages & Frameworks > Flutter or search for Flutter and change Flutter SDK path.
  • Copy the absolute path of fvm symbolic link in your root project directory. Example: /absolute-project-path/.fvm/flutter_sdk
  • Apply the changes.
  • Restart Android Studio to see the new settings applied.

Make sure FVM configured correctly.

0
On

If you are running macOS, open Activity Monitor and terminate Dart (quite sure that it take up ~90% CPU). This disables the dart:async library and makes the fan run freaking loud

0
On

Fresh Flutter install resolved this in local env, IDE restart wasn't enough

https://stackoverflow.com/a/77996890/