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

17

8

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

 }
Prolusion answered 20/10, 2020 at 12:46 Comment(7)
try to import dart:asyncPash
@LOfG not making any differenceProlusion
Which is the line where you get the error?Amphipod
@MiguelRuivo all the words "Future" are underlined in red, IDE probably can't recognize itProlusion
What if you import dart:async; ?Amphipod
@MiguelRuivo I tried that already, didn't workProlusion
Please don't add "solved" to your title or question. Instead, mark an answer correct by clicking on the checkmark. You can also add your own answer and accept it if none of the ones you received solved your problem.Scalable
H
4

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"

Hacienda answered 22/6, 2021 at 12:12 Comment(0)
B
12

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"

Bursar answered 22/10, 2020 at 12:55 Comment(1)
My SDK version was like that, but executing flutter clean and flutter pub get solves that!Isleana
D
9

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.

Dentate answered 20/10, 2020 at 13:38 Comment(1)
I tried restarting the IDE a couple of times but that didn't work, and as per the thread you've linked, re-installing might resolve the issue but that's the last thing to do, so I'm trying not to re-install.Prolusion
E
6

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.

Eudosia answered 10/2, 2021 at 15:49 Comment(0)
H
4

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"

Hacienda answered 22/6, 2021 at 12:12 Comment(0)
L
4

after updating flutter version, i faced this issue.

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

Lordling answered 22/7, 2023 at 16:38 Comment(0)
C
3

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

Census answered 3/1, 2021 at 6:16 Comment(0)
P
3

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.

Pyrophyllite answered 31/5, 2023 at 16:20 Comment(0)
E
1

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",
Etam answered 21/5, 2023 at 19:24 Comment(0)
C
0

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

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

Cates answered 20/10, 2020 at 13:48 Comment(2)
I tried that a couple of times, didn't work for me :/Prolusion
I actually started it all over now 😅 I'll let you know when I face the same problem again. Thanks for your concernProlusion
B
0

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

Barsky answered 1/7, 2021 at 21:34 Comment(0)
A
0

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.

Apostolic answered 11/3, 2022 at 16:37 Comment(0)
K
0

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.

Kinzer answered 31/5, 2023 at 12:23 Comment(0)
E
0

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.

Extensor answered 11/7, 2023 at 6:11 Comment(0)
E
0

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

Eliciaelicit answered 24/7, 2023 at 7:53 Comment(0)
C
0

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

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

Camillecamilo answered 14/2, 2024 at 19:48 Comment(0)
O
0

For me this error about "Future isn't a type" showed up after I upgraded flutter in Android Studio Koala. Probably some error happened while upgrading and my project "lost" the path for flutter sdk. What solved the problem for me was:

  • Go to "SDK Manager" (in Android Studio is File>Settings...)
  • Then went to "Language & Frameworks"
  • Clicked in "Flutter"
  • Then in "Flutter SDK path" pressed the "..." and there selected the path that my flutter SDK was.

(Then you can close your project and open it again, just in case, but for me wasn't needed)

Here's a screenshot of settings from Android Studio's interface where you can change the SDK path for flutter

Orvas answered 21/8, 2024 at 13:30 Comment(0)
P
-1

Close the ide and reopen it, It worked

Possum answered 15/4, 2021 at 13:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.